Hello there, I have a question regarding annotatio...
# getting-started
a
Hello there, I have a question regarding annotations. What is the difference between
Copy code
object Test {
  @get:Composable
  val primary: Color get() = TODO()
}
and
Copy code
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
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
and where is it being applied in the former?? I assume the getter as well, No?
k
It is applied to the property or the backing field. You can try with the following
Copy code
@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
I think you are confusing
Copy code
object Test {
  @get:Composable  // Has @get:
  val primary: Color get() = TODO()
}
with
Copy code
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?
Copy code
@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
Sorry I misread the original post and answered the wrong question. I will leave this for someone who knows more.
a
No worries mate. I do appreciate the effort
r
@get:
annotates a java getter. can it be that Compose scans kotlin code?
a
if
@get:
annotates the java getter, what does
Copy code
val p @Annotation get() = ...
annotate??
r
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
Then it is more of a Compose scan rather than an Annotations issue, right??
r
that was my guessing..