eclipse-archived / ceylon

The Ceylon compiler, language module, and command line tools
http://ceylon-lang.org
Apache License 2.0
399 stars 62 forks source link

Allow union types in annotations parameters #5826

Open thradec opened 8 years ago

thradec commented 8 years ago

I thought, that String|Integer will fulfill spec condition

Each initializer parameter of an annotation type must have one of the following types:

  • Integer, Float, Character, or String

but I got error "illegal annotation parameter type: 'String|Integer'", can we support this?

gavinking commented 8 years ago

Well, no, a union type isn't one of those listed types ;)

This is probably quite hard to implement FTR.

xkr47 commented 8 years ago

Perhaps generate bytecode that uses separate parameters, one for each type with different name suffixes, that are transparently merged to appear as one in ceylon?

gavinking commented 8 years ago

Annotation args can't be null in Java :-(

tombentley commented 8 years ago

You'd need a "discriminator" annotation type member at the java level, to know which was actually chosen at the use-site, and then to provide "dummy" arguments for the rest. Then at runtime you could get the value of the member selected by the discriminator and ignore the rest. It would be awkward to use from Java.

What's your use case @thradec?

gavinking commented 8 years ago

Well that's one way. The other way is one I suggested a long time ago which is to serialize the value to a json string. Or perhaps even to a base64-encoded binary string. That's a more general solution that would accommodate many more types.

thradec commented 8 years ago

What's your use case @thradec?

For parameterized tests, I thought it would be comfortable for simple scenarios with few values, specify them directly in annotation, eg.

test
shared void fibonacci(
    parameters({1, 2, 3, 4, 5, 6}) Integer input,
    parameters({1, 1, 2, 3, 5, 8}) Integer result) => ...

(I know, I can workaround it with parameter for each supported type, just it won't be so nice ;-))