0
votes

when i use Jetpack compose:

@Composable
fun parentFuncNeedComp( func:@Composable ()->Unit){
    func()
}

I can't move @Composable before func like:

@Composable func: ()->Unit

It said: This annotation is not applicable to target 'value parameter'

And I make a custom annotation named MyAnnotation, and I code a example:

@MyAnnotation
fun func1() {

    println("func1")
}
fun func2() {
    println("func2")
}

fun parentFunc(@MyAnnotation func:@MyAnnotation ()->Unit){
    func()
}

fun main() {
    parentFunc(::func2)
    parentFunc(::func1)
}

Why both of them work?! And what' s the diff of the @MyAnnotation location?

1

1 Answers

0
votes

You can specify the allowed targets of an annotation in Kotlin using the @Target meta-annotation:

@Target(AnnotationTarget.VALUE_PARAMETER)
annotation class MyAnnotation

You can read more about the various meta-annotations for annotations in Kotlin at kotlinlang.org.