如何为Dart编写注释 [英] how can I write an annotation for Dart
问题描述
问题:
- 实现注释的过程是什么.
- 如何或实际上什么时候可以激活您开发的注释?
我似乎找不到有关如何编写用于实现.
I can't seem to find an example or tutorial on how to write a class to implement annotations for .
例如,对于Java,可能会有一个注释,该注释表示在编译时调用的类,并允许您修改或注入代码. 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?
在理解Dart生态系统的这一领域方面,我做了一些(进一步的)挖掘.我要添加一些注释,因为注释可以提供强大的功能,并提供有关如何使用注释的透明注释.
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.
查看Dart的一些实际注释后,Dart注释会记录"符号"(标签或 metadata 标签).问题在于如何在Dart中使用注释.
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.
基于对代码的了解,我目前的理解是它们是类对象上的标记.注释似乎是高度非结构化因为注释可以简单地声明,但是没有使用或识别标签的结构(又名 annotation ).
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).
注释步骤
- 标识要标记的属性或操作.
- 需要编写代码以使用或'工作'您的注释.查看类似 观察例如,strong> .
- 您可以实现和测试LOAD-time代码,以查找和处理标签.我看不到注册注释和提供处理程序的基础架构.
- 这是通过库中的main()方法完成的.
- Identify the property or action you want to label.
- Need to write code to use or 'work' your annotation. Look at something like Observe as an example.
- 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.
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.
读数:
- Dart语言规范
- 类型注释
- 元数据
- Dart Language Specification
- type annotations
- metadata
- extending a class
示例:
- 观察
- Observe
- Observable.dart
推荐答案
任何带有const构造函数的类都可以用作注释.
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); }
仅此而已.
另请参见 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" }
或者,您可以直接处理源.例如,dart2js编译器具有一个源镜像"库,该库可反映源结构. dart2js和分析器会执行此操作以了解"proxy"注释.
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.
这篇关于如何为Dart编写注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Observe