https://kotlinlang.org logo
p

Pavel Sidyakin

12/01/2020, 1:35 PM
Hello! Is it possible to create a typealias for an annotation? I want to do something like this (the code doesn't compile):
Copy code
import androidx.annotation.FloatRange

typealias ProgressFloatRange = FloatRange(from = 0.0, to = 1.0, fromInclusive = true, toInclusive = true)
t

Tobias Berger

12/01/2020, 2:42 PM
1. I don't think what you're trying to do is possible 2. This is not how a typealias works. A typealias just creates a different name for the same thing, You could do
typealias MyAnnotation = FloatRange
, but you can't sorte an Annotation "instance" in a typealias (or anything else)
👆 1
m

Matteo Mirk

12/07/2020, 8:54 AM
Not with a typealias but I think you can create a new annotation that includes that annotation usage
Copy code
@FloatRange(from = 0.0, to = 1.0, fromInclusive = true, toInclusive = true)
annotation class ProgressFloatRange
the applicability depends on FloatRange retention and target, obviously
oh… now I see it’s something specific of Android SDK so no, I don’t think this can be done 😕
t

Tobias Berger

12/07/2020, 9:04 AM
you see stuff like that quite often, esp. in things like Spring. But that's not a direct language feature. The code that analyzes the annotations has to be designed to look for meta annotations.
m

Matteo Mirk

12/07/2020, 9:04 AM
yeah I realized afterwards that you have no control over this, sorry