Siegfried Kiermayer
09/06/2022, 4:50 PM@field: Named(value = "approvals")
actually works. No clue what this should doRoukanken
09/06/2022, 4:54 PMclass FooBar {
@Annotation
some: Field;
}
is applied to property
target by default, and that one is not visible to java at allephemient
09/06/2022, 5:01 PMparam
, property
, or field
if you don't tell itephemient
09/06/2022, 5:03 PMjavax.inject.Named
, that's a Java annotation so it can't be @property:Named
, but since it doesn't have other restrictions it could be both @param:Named
and @field:Named
, so it defaults to the formerephemient
09/06/2022, 5:05 PMclass FooBar @Inject constructor(
@Named("approvals")
private val approvalsProcess: Process<out Model>
) {
if you really need field injection, that's one of the cases for lateinit var
, but val
for something that will be mutated is not goodSiegfried Kiermayer
09/06/2022, 5:06 PMSiegfried Kiermayer
09/06/2022, 5:08 PMephemient
09/06/2022, 5:10 PMapprovalsProcess
translates into several different things:
• a parameter in the primary constructor
• a property, which is made of
◦ a getter, and
◦ a backing field
"property" isn't a concept in Java, so @property:
can only be used by (and can only be seen by) Kotlin annotations, but @param:
@get:
@field:
are otherwise possible thereephemient
09/06/2022, 5:11 PMSiegfried Kiermayer
09/06/2022, 5:11 PMephemient
09/06/2022, 5:13 PMlateinit var
in Kotlin - var
since they're definitely mutated, and lateinit
which indicates to the compiler both that it isn't initialized in the constructor, and also the field should be exposed to Java/reflectionRoukanken
09/06/2022, 5:13 PMclass Example {
var stringRepresentation: String
get() = this.toString()
set(value) {
setDataFromString(value)
}
}
ephemient
09/06/2022, 5:14 PMephemient
09/06/2022, 5:14 PMSiegfried Kiermayer
09/06/2022, 5:15 PMephemient
09/06/2022, 5:15 PMvar foo // has backing field
var foo // has backing field because it's mentioned
get() { println("getting $field"); return field }
set(value) { field = value }
var foo // has no backing field because it's not mentioned
get() = 1
set(value) { /* ignore */ }