【问题标题】:Dart Decorators Like Typescript像打字稿一样的飞镖装饰器
【发布时间】:2022-01-15 09:54:36
【问题描述】:

我是 Typescript 后台开发人员,我可以像 typescript 一样在 dart 或 kotlin 中创建装饰器吗?
喜欢这段代码:

  const enumerable = (value: boolean) => {
  return (target: any, memberName: string, propertyDescriptor: PropertyDescriptor) => {
    propertyDescriptor.enumerable = value;
  }
}

【问题讨论】:

    标签: typescript kotlin dart decorator


    【解决方案1】:

    dart 语法中的相同代码是:

    final enumerable = (bool value) {
      return (Object target, String memberName, PropertyDescriptor propertyDescriptor) {
        propertyDescriptor.enumerable = value;
      };
    };
    

    在 kotlin 语法中应该是:

    val enumerable = { value: Boolean -> 
        { target: Object, memberName: String, propertyDescriptor: PropertyDescriptor -> 
            propertyDescriptor.enumerable = value
        } 
    }
    

    【讨论】:

    • 可以像@enumerable一样使用吗?
    • 我不确定你的意思。你能进一步解释一下吗?
    • 像这样 const enumerable = (value: boolean) => { return (target: any, memberName: string, propertyDescriptor: PropertyDescriptor) => { propertyDescriptor.enumerable = value; } } 类人 { firstName: string = "Jon" lastName: string = "Doe" @enumerable(true) getFullName () { return ${this.firstName} ${this.lastName}; }
    • 在这种情况下,不,在 dart 中,您可以使用任何具有 const 构造函数的类作为注释,而在 kotlin 中,您将定义一个 annotation class。但都不支持使用函数/函数字面量作为注解。
    猜你喜欢
    • 2018-06-21
    • 2019-07-29
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 2018-02-12
    • 2020-12-24
    • 2020-01-02
    • 2020-01-31
    相关资源
    最近更新 更多