This Content is from Stack Overflow. Question asked by SinaMobasheri
I’m trying to define annotation with a type parameter
annotation class BotCommandScopeChat<T>( val type: String = "chat", val chatId: T, )
chatId can be in form of
but intellj says Invalid type of annotation member.
so what I’m doing wrong?
Kotlin annotations can only use the following as construction parameters:
- Types that correspond to Java primitive types (Int, Long etc.)
- Classes (
- Other annotations
- Arrays of the types listed above
Generic types are not allowed.
In your situation you’ll either have to make two constructors, one for each type:
annotation class BotCommandScopeChatIntId( val type: String = "chat", val chatId: Int, ) annotation class BotCommandScopeChatStringId( val type: String = "chat", val chatId: String, )
Or have two constructor parameters, one for each type:
annotation class BotCommandScopeChat( val type: String = "chat", val chatIdString: String? = null, val chatId: Int? = null, )
These will have to have default parameters, else users will always have to provide one, even if they don’t need to.
Or (and this would be my preference), just use a String, and convert it to an Int if necessary.
annotation class BotCommandScopeChat( val type: String = "chat", val chatId: String, )
This Question was asked in StackOverflow by SinaMobasheri and Answered by aSemy It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.