Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Types as annotation arguments #39

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

abreslav
Copy link
Contributor

Currently, we can only mention classes in annotation arguments: @Ann(Foo::class). Although in some contexts terms class and type can be used interchangeably, those are two different things, for example, a class Foo can be mentioned in many different types: Foo, Foo<Bar>, Foo<Foo<Bar>>, Foo<*>, `Bar, etc.

Sometimes we need to refer to types, not classes. For example, in many cases it's useless to say List::class, we need more information like List<String> or List<Nothing>.

We propose adding support for types as annotation arguments (and, optionally, elsewhere in the language):

  • an annotation parameter may be declared to be of type KType,
  • an annotation argument may be a type (in some syntactic form, e.g. @Ann(Foo<Bar>)).

Example (syntax is subject to discussion):

annotation class Ann(val type: KType)

@Ann(type = Foo<Bar>)
fun test() {} 

This is relevant for #38

@damianw
Copy link

damianw commented Sep 29, 2016

How would this be represented in Java?

This might also be a good opportunity to bring up built-in type literals for the rest of the language, to replace the java-style hacks like TypeLiteral and TypeToken. I'd think that if I can represent a type literal in an annotation, I should be able to do so in code as well.

val listOfStringType = List<String>::type

@abreslav
Copy link
Contributor Author

How would this be represented in Java?

We have two options:

  • represent such types as strings, e.g. "Foo<Bar>"
  • represent them as annotations, e.g. @Ann(type = @Type(name = "Foo", args = {@Type(name = "Bar)})`

This might also be a good opportunity to bring up built-in type literals for the rest of the language

Fair point.

@udalov udalov mentioned this pull request Aug 2, 2017

### `typeof`

Something like `@Ann(typeof(Foo<Bar>))`. Note: `typeof` is [reserved as a keyword](https://github.com/JetBrains/kotlin/blob/master/compiler/frontend/src/org/jetbrains/kotlin/lexer/Kotlin.flex#L249) already.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This link is dead. Permalinks should be used instead to prevent future occurrence.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants