简体   繁体   English

如何使用闭包编译器在javascript中注释扩展泛型类的类

[英]How to annotate in javascript a class that extends a generic type, using closure compiler

I created a Class factory in javascript that takes type T and generates a new enhanced class of type T, simple. 我在javascript中创建了一个类工厂,它采用类型T并生成一个类型为T的新增强类,简单。

I'm using google's closure compiler to compile javascript, and closure's annotation to annotate Types etc. The challenge is how to annotate a Class that extends type T, where T is a generic type. 我正在使用谷歌的闭包编译器来编译javascript,并使用闭包的注释来注释类型等。挑战是如何注释扩展类型为T的类,其中T是泛型类型。

Here is a simplified Class Enhanced that extends another type: 这是一个简化的Class Enhanced ,它扩展了另一种类型:

/**
 * @constructor
 */
function Foo(){
    this.x = 'x';
};

/**
 * @constructor
 */
function Bar(){
    this.y = 'y';
};

/**
 * @constructor
 * @param {function(new:T)} FooOrBar
 * @extends {T}
 * @template T
 */
function Enhanced(FooOrBar){
    FooOrBar.call(this);
    this.z = 'z';
};

/** @type Enhanced.<Foo> */
var enhancedFoo = new Enhanced(Foo);

/** @type Enhanced.<Bar> */
var enhancedBar = new Enhanced(Bar);

when I compile this code with the closure compiler I get a compiler warning: 当我用闭包编译器编译这段代码时,我得到一个编译器警告:

'Could not resolve type in @extends tag of Enhanced'. '无法解析增强型的@extends标签中的类型。

So, obviously the compiler does not know from where to infer T when this class is compiled. 因此,显然编译器在编译此类时不知道从哪里推断T.

Can someone please point out why the compiler can't infer the type when the class is created, or if there is a way to annotate a generated class as extending type T. 有人可以指出为什么编译器在创建类时无法推断出类型,或者是否有办法将生成的类注释为扩展类型T.

my use case is Class factories where the classes cannot be annotated because they're created at runtime 我的用例是类工厂,其中的类无法注释,因为它们是在运行时创建的

It sounds like the Closure Compiler may not be able to help you out here, at least not with this particular approach. 听起来Closure Compiler可能无法帮助你,至少不是这种特殊的方法。

You intend Enhanced to be a function that takes a class and returns a class. 您希望Enhanced是一个接受类并返回类的函数。 There are certainly languages in which operate on classes in this way (Python, Javascript, Ruby, etc.). 当然有一些语言以这种方式对类进行操作(Python,Javascript,Ruby等)。

But the Closure Compiler requires static classes in order to do its static analysis. 但Closure Compiler需要静态类才能进行静态分析。 That isn't necessarily the way that it has to work, but that is the way it was designed to work. 这不一定是它必须工作的方式,但这是它的工作方式。

A rough rule of thumb is this: if you can represent the types in Java, you can represent the types in Closure annotations. 一个粗略的经验法则是: 如果您可以用Java表示类型,则可以在Closure注释中表示类型。

Of course, this isn't GWT; 当然,这不是GWT; you're not writing Java. 你不是在写Java。 You can use your JS-fu to write all the meta classes you want. 您可以使用JS-fu编写所需的所有元类。 But don't expect the Closure Compiler to necessarily be able to type check it. 但是不要指望Closure Compiler必须能够键入检查它。


The good news is that there is literally decades of research and best practices behind languages following this paradigm of static classes: C++, Java, C#. 好消息是,在这种静态类范例之后,语言背后有数十年的研究和最佳实践:C ++,Java,C#。 If you are familiar with these, you might ask yourself "How would I do this in those languages?" 如果您熟悉这些,您可能会问自己“我将如何使用这些语言?”

Your actual problem (whatever it is) has likely been solved hundreds or thousands of times by a design pattern for these OO languages, and you could do the same. 您的实际问题(无论是什么)可能已经通过这些OO语言的设计模式解决了数百次或数千次,您也可以这样做。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在javascript中扩展抽象类并为闭包编译器添加注释,但没有闭包库? - How do you extend an abstract class in javascript and annotate for closure compiler but without closure library? 如何使用 Google Closure Compiler 注释可选参数? - How to annotate an optional parameter using Google Closure Compiler? Closure Compiler-无法解析@extends标记中的类型 - Closure Compiler - Could not resolve type in @extends tag 如何在javascript中为Closure Compiler注释嵌套对象,并且所有属性都是可选的? - How to annotate nested object in javascript for Closure Compiler and have all properties optional? 使用“类”容器对象时,Closure Compiler @extends不起作用 - Closure Compiler @extends doesn't work when using “Class” container objects 是否可以为闭包编译器添加@language ECMASCRIPT5来注释JavaScript? - Is it possible to annotate JavaScript for the Closure Compiler adding @language ECMASCRIPT5? Closure Compiler如何使用类型信息编译为更快的JavaScript? - How does Closure Compiler use type information to compile to faster JavaScript? 如何在JavaScript中为Google闭包编译器设置参数类型? - How can I set type of arguments in JavaScript for Google closure compiler? 与Closure编译器一起使用的JavaScript类的实现 - Implementation of JavaScript Class that works with Closure Compiler 是否包含使用Google Closure编译器的Ecmascript 6类? - Include an Ecmascript 6 class using Google Closure Compiler?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM