如何为 Dart 编写注释 [英] how can I write an annotation for Dart

查看:28
本文介绍了如何为 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 注释记录了一个 notation"(标签或 元数据 标签).问题是关于如何在 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.

基于查看代码位,我目前的理解是它们是类对象上的标记.看起来注释是高度非结构化 因为虽然可以简单地声明注释,但没有结构可以使用或识别标签(又名 注释).

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).

注解步骤

  1. 确定要标记的属性或操作.
  2. 需要编写代码以使用或工作"您的注释.看看类似观察 为例.
  3. 您可以实施和测试 LOAD 时间代码来查找和处理您的标签.例如,我没有看到用于注册注释和提供处理程序的基础结构.
    • 这是通过库中的 main() 方法完成的.

至少我认为它是这样工作的.Dart 语言规范 在这方面.

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 语言规范
  • 类型注释
  • 元数据

    示例:

    • 观察

      推荐答案

      任何带有 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" 
      }
      

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

      或者,您可以直接处理源.例如,dart2js 编译器有一个反映源结构的源镜像"库.这就是 dart2js 和分析器为理解代理"注释所做的工作.

      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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆