Allow inline metadata instantiation sans parens #13582
Labels
area-language
Dart language related items (some items might be better tracked at github.com/dart-lang/language).
closed-stale
Closed as the issue or PR is assumed stale
type-enhancement
A request for a change that isn't a bug
This issue was originally filed by @seaneagan
Dart supports inline instantiation of annotations, but parens are required even when there are no arguments:
@foo()
int x;
which is ugly to someone coming from Java where parens are not required, so annotation designers often create top-level constants instead. Examples:
@deprecated
@OverRide
@Proxy
This is something that would probably need to be added to the style guide to keep annotations consistent. Also, since metadata is const, the style guide currently requires these to be:
@deprecated
@OverRide
@Proxy
so they would either need to be renamed, or an exception for metadata would need to be added to the style guide. And should the constants still be created if the annotation class has optional arguments, in which case the user needs to decide which to use:
@foo
@foo()
which can't be avoided if the annotation designer wants to add optional arguments after the fact. And refactoring between the 2 forms is more difficult, since the casing of the first letter needs to change ( e.g. @foo -> @Foo(bar) ).
Solution:
Allow @Foo as shorthand for @Foo()
Then for example we could do:
@deprecated
class Foo {}
and if we wanted to use a hypothetical optional argument, we just add parens:
@deprecated(use: Bar);
class Foo {}
This would mean that if you want to use a Type literal as an annotation, you would need to first assign it to a top-level constant, but that seems like an edge case.
The text was updated successfully, but these errors were encountered: