https://kotlinlang.org logo
Title
a

andylamax

10/31/2021, 7:40 AM
Hello there, I have a question regarding annotations. What is the difference between
object Test {
  @get:Composable
  val primary: Color get() = TODO()
}
and
object Test {
  val primary: Color
  @Composeable
  get() = TODO()
}
Compose seem to be happy with the later and not with former. I though these where same. Anyone?
k

knthmn

10/31/2021, 9:48 AM
In the latter the annotation is applied on the getter function. If you check
@Composable
, it is only applicable on
AnnotationTarget.FUNCTION, AnnotationTarget.TYPE, AnnotationTarget.TYPE_PARAMETER, AnnotationTarget.PROPERTY_GETTER
a

andylamax

10/31/2021, 10:12 AM
and where is it being applied in the former?? I assume the getter as well, No?
k

knthmn

11/01/2021, 7:43 AM
It is applied to the property or the backing field. You can try with the following
@Target(AnnotationTarget.FIELD)
annotation class FieldAnnotation
@Target(AnnotationTarget.PROPERTY_GETTER)
annotation class GetterAnnotation
@Target(AnnotationTarget.PROPERTY)
annotation class PropertyAnnotation

class Test {
    @FieldAnnotation val x1 = 0
    val x2 @FieldAnnotation get() = 0  // compilation error

    @GetterAnnotation val x3 = 0  // compilation error
    val x4 @GetterAnnotation get() = 0

    @PropertyAnnotation val x5 = 0
    val x6 @PropertyAnnotation get() = 0  // compilation error
}
a

andylamax

11/01/2021, 8:30 AM
I think you are confusing
object Test {
  @get:Composable  // Has @get:
  val primary: Color get() = TODO()
}
with
object Test {
  @Composable // No @get:
  val primary: Color get() = TODO()
}
The later indeed applies to the backing field. What does the former do?
In other words, would this cause a compilation error? if so, why?
@Target(AnnotationTarget.FIELD)
annotation class FieldAnnotation
@Target(AnnotationTarget.PROPERTY_GETTER)
annotation class GetterAnnotation
@Target(AnnotationTarget.PROPERTY)
annotation class PropertyAnnotation

class Test {
    @get:GetterAnnotation val x3 = 0  // Getter annotation, applied on field with a 'get' directive
    val x4 get() = 0
}
k

knthmn

11/01/2021, 3:20 PM
Sorry I misread the original post and answered the wrong question. I will leave this for someone who knows more.
a

andylamax

11/01/2021, 6:37 PM
No worries mate. I do appreciate the effort
r

Rein T

11/09/2021, 9:35 AM
@get:
annotates a java getter. can it be that Compose scans kotlin code?
a

andylamax

11/10/2021, 3:56 AM
if
@get:
annotates the java getter, what does
val p @Annotation get() = ...
annotate??
r

Rein T

11/10/2021, 10:26 AM
I agree that the normal behavior is exactly as you are stating it. I verified with moving
@get:Size
to a
get()
in one of my project and it works just as good. What I was thinking is that your problem is linked to how Composable scans the kotlin code and not applicable to
@AnyAnnotation
a

andylamax

11/10/2021, 9:31 PM
Then it is more of a Compose scan rather than an Annotations issue, right??
r

Rein T

11/11/2021, 8:32 AM
that was my guessing..