https://kotlinlang.org logo
#getting-started
Title
# getting-started
j

Jason5lee

01/18/2022, 7:28 AM
How to define a internal
var
property that only its
get
is
PublishedApi
?
It’s a field-like property. Get and set just delegate to the backing field.
j

Joffrey

01/18/2022, 8:12 AM
Have you tried the syntax
@get:PublishedApi
to annotate only the getter?
v

Vampire

01/18/2022, 8:55 AM
Otherwise have a public val that delegates to a private var.
j

Joffrey

01/18/2022, 8:57 AM
But that would be quite different, the getter would be more visible than a published internal and the setter less visible than an internal
v

Vampire

01/18/2022, 9:33 AM
Ok, I might have misinterpreted "published api". Well then a published api internal val delegating to a non-published api internal var. But if just annotating the getter works, then of course that, you just didn't sound confident that it will work, just that it maybe could work. 🙂
j

Joffrey

01/18/2022, 9:38 AM
Well then a published api internal val delegating to a non-published api internal var
Yep, that makes sense if the annotation is not supported on the getter only
you just didn't sound confident that it will work
Yeah I'm not confident because I never played with
@PublishedApi
directly, so I prefer to write with caution 🙂 but I see no reason why it shouldn't work
j

Jason5lee

01/18/2022, 9:42 AM
I rewrote it into a
@PublishedApi
function that returns the value of a private var. Thanks for the answers.
j

Joffrey

01/18/2022, 9:45 AM
Did annotating the getter not work?
Copy code
@get:PublishedApi
internal var x: Int = 42
Or did you just prefer with a function?
v

Vampire

01/18/2022, 9:55 AM
So basically my initial suggestion with added boilerplate 😄
😄 1
j

Jason5lee

01/18/2022, 9:58 AM
I just preferred. I might test that later.
3 Views