Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I write an annotation for Dart

The question:

  • What is the procedure to implement an annotation.
  • How can, or indeed when can one activate the annotation you developed?

I can't seem to find an example or tutorial on how to write a class to implement annotations for dart.

For example with Java you might have an annotation that represents a class that is invoked at compile time and let you modify or inject code. Do Dart annotations work like this as well?

background

I have done some (further) digging on understanding this area of the Dart ecosystem. I'm adding some notes because annotation can be a powerful with transparent commentary on how to use it.

After looking at some actual annotation from Dart, Dart annotations record "a notation" (a label or metadata tags). The question is about how to uses annotations within Dart.

gloaming

My current understanding, based on looking at bits of code, is that they are markers on class objects. It looks like annotations are highly-unstructured since while an annotation can be declared simply, there's no structure to use or recognise a label (aka annotation).

steps of annotation

  1. Identify the property or action you want to label.
  2. Need to write code to use or 'work' your annotation. Look at something like Observe as an example.
  3. You can implement and test LOAD-time code to look-for and process your labels. I don't see an infrastructure to register an annotation and provide handlers for example.
    • This is done via the main() method in your library.
  4. Implement and test annotation behaviour.

At least I think that's how it works. There's not really a lot of information in the Dart language specification on this area.

Observation and inspection raised a few general questions as well. I've left a reading list of sorts and examples, to assist others in joining the exploration.

readings:

  • Dart Language Specification
  • type annotations
  • metadata
    • extending a class
  • I love Dart annotations
  • zones
    • zone class
  • Dart Annotations are No Longer Structured

examples:

  • Observe
    • Observable.dart
  • annotations.dart
like image 966
will Avatar asked May 04 '14 12:05

will


People also ask

What is type annotation in Dart?

The Dart language is type safe: it uses a combination of static type checking and runtime checks to ensure that a variable's value always matches the variable's static type, sometimes referred to as sound typing. Although types are mandatory, type annotations are optional because of type inference.

How do you use Dart metadata?

In dart a metadata annotation starts with @ symbol, followed by a call to a constant constructor or a compile-time constant such as override. @override and @deprecated are available to all dart codes.

What is reflection in Dart?

Reflection in Dart is based on the concept of mirrors. A mirror, in this context, is an object that reflects another object. Mirrors are a particular form of reflective APIs that have advantages with respect to distribution, deployment and security, which we will discuss in depth in Section 7.2.

What is meta flutter?

meta library Null safety. Annotations that developers can use to express the intentions that otherwise can't be deduced by statically analyzing the source code.


1 Answers

Any class with a const constructor can be used as annotation.

const FOO = const Foo(37);

@Foo(42)
class Foo {
  @Deprecated("until further notice");
  final int x;
  @FOO
  const Foo(this.x);
}

There is nothing more to it.

See also https://www.dartlang.org/docs/dart-up-and-running/contents/ch02.html#ch02-metadata

Metadata doesn't do anything by itself. If your program wants to read metadata off a class, it needs to use mirrors.

import 'dart:mirrors';
const tag = "TAG"; 

@tag class C {}
void main() {
  print(reflectClass(C).metadata.first.reflectee);  // prints "TAG"
  var c = new C();
  print(reflect(c).type.metadata.first.reflectee);  // prints "TAG" 
}

See: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart-mirrors.ClassMirror#id_metadata

Alternatively, you can process the source directly. For example, the dart2js compiler has a "source mirror" library that reflects over the source structure. It is what dart2js and the analyzer do to understand the "proxy" annotation.

like image 59
lrn Avatar answered Sep 30 '22 11:09

lrn