Skip to content

Allow inline metadata instantiation sans parens #13582

Closed as not planned
Closed as not planned
@DartBot

Description

@DartBot

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).closed-staleClosed as the issue or PR is assumed staletype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions