Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attributes in Dart

Tags:

dart

Are there any plans to introduce attributes
for classes, methods, parameters of methods,
something like C# or Java attributes ?

    [Test]
    class SomeClass
    {
        [Test]
        someMethod()
    }


or

    @Test
    class SomeClass
    {
        @Test
        someMethod(@Test int param)
    }

For many frameworks it would be very useful

like image 945
karol.dev.os Avatar asked Nov 06 '12 20:11

karol.dev.os


3 Answers

In dart, they are called metadata / annotation. The syntax is quite close to java. Here's a example :

@Test testMethod() {}

In Dart Specification you can read :

Metadata consists of a series of annotations, each of which begin with the character @, followed a constant expression that starts with an identifier. It is a compile time error if the expression is not one of the following:

  • A reference to a compile-time constant variable.
  • A call to a constant constructor.

[....]

Metadata can appear before a library, class, typedef, type parameter, constructor, factory, function, field, parameter, or variable declaration and before an import or export directive.

There're already some annotations predifined in dart:core. Particulary @override, @deprecated and @proxy.

like image 176
Alexandre Ardhuin Avatar answered Sep 23 '22 07:09

Alexandre Ardhuin


Dart already has annotations, similar to Java in some ways, they're just not used in very many places yet, and they're not accessible from reflection yet either.

See this article: http://news.dartlang.org/2012/06/proposal-to-add-metadata-to-dart.html

like image 29
Justin Fagnani Avatar answered Sep 21 '22 07:09

Justin Fagnani


Here's a brief introduction to the two metadata annotations currently available in the Dart meta library:

Dart Metadata is your friend.

This doesn't preclude you from using your own, but these are the two that have tooling integration with the Dart Editor.

like image 36
John Evans Avatar answered Sep 19 '22 07:09

John Evans