使用Closure-Compiler键入检查数组内容 [英] Type Checking Array Contents with Closure-Compiler

查看:90
本文介绍了使用Closure-Compiler键入检查数组内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Google Closure中,如果特定 @type {Array。< type>} 数组是初始化后,我可以确定Google Closure是否会确认数组内容?

In Google Closure, if an Array of a specific @type {Array.<type>} is initialized, can I be sure that Google Closure will confirm the Array contents?

这是一个小测试用例。在我看来, {Array。< string>} 正在偷偷溜过 {Array。< number>} 检查,但同一项检查正确阻止 {string} 。我对GC有点新手,这是我的错误吗?

Here is a small test case. It appears to me that an {Array.<string>} is sneaking past an {Array.<number>} check, although a {string} is correctly blocked by the same check. I am a little new to GC, is this an error on my part?

我已将其粘贴到 Google Closure Service ,我只展示了两个预期错误中的一个(2013年9月12日)。我在本地jar文件(最新版本,v20130823)上使用 ADVANCED_OPTIMIZATIONS warning_level VERBOSE 对此进行了双重测试。它仍然看起来像 {Array。< string>} 偷偷靠近。

I've pasted this to the Google Closure Service, and I'm showing only one of two expected errors (Sept 12 2013). I've double-tested this on my local jar file (newest, v20130823) with ADVANCED_OPTIMIZATIONS and warning_level VERBOSE. It still looks like the {Array.<string>} sneaks by.

文档:为Google Closure添加注释

提前感谢您的意见。

// ==ClosureCompiler==
// @output_file_name default.js
// @compilation_level ADVANCED_OPTIMIZATIONS
// @warning_level VERBOSE
// @language ECMASCRIPT5
// ==/ClosureCompiler==

/**
    no warning, as expected:
    @type {Array.<number>}
*/
var a = [1,2,3,4];

/**
    warning! Type mismatch as expected:
    @type {Array.<number>}
*/
var b = 'mismatch';    // {string} does not match {Array.<number>}

/**
    no warning, but Type mismatch was expected:
    @type {Array.<number>}
*/
var c = ['foo','bar']; // {Array.<string>} should not match {Array.<number>}


// prevent compile-to-zero
alert(a);
alert(b);
alert(c);

注意:
我仔细看了一下此相关问题,其中Array.push()的类型为手动填写。但这个问题涉及初始化。如果我采用他更正的代码并使用垃圾数据初始化他的所有数组,如上所述,GC也无法在他的情况下捕获垃圾。

note: I've taken a close look at this related question, where the type of Array.push() was manually filled in. This question concerns initialization, though. If I take his corrected code and init all of his arrays with garbage data, as above, GC fails to catch the garbage in his case as well.

已编辑: warning_level VERBOSE 语言ECMASCRIPT5 添加到测试用例的标题中,以确定。 {Array。< string>} 仍未检测到。

Edited: added warning_level VERBOSE and language ECMASCRIPT5 to the header on the test case, just to be sure. {Array.<string>} still not detected.

推荐答案

这是当前类型检查器的限制。右边有输入 Array (又名数组<?> )而不是数组< number> 数组< string> ,允许分配给任何类型。

This is a limitation of the current type checker. The right had side is typed as "Array" (aka Array<?>) not "Array<number>" or "Array<string>" respectively, which is allowed to be assigned to any type.

对于这种特定情况,可以增强类型类型检查器,但由于事实数组在JavaScript中是可变的,因此在更复杂的情况下会很快崩溃。也就是说,如果[11]键入 Array< number> ,那么添加任何其他类型都是非法的,但数组通常不是同质的考虑:

It is possible to enhance to type type checker for this specific case, but it quickly breaks down in more complex cases due to the fact arrays are mutable in JavaScript. That is if "[11]" were typed to Array<number> then adding any other type would be illegal but arrays are generally not homogeneous consider:

var args = [];
args[0] = 1;
args[1] = 'foo';

因此默认情况下,Array的类型不能限于初始类型。

So the type of Array must not be limited to initial type by default.

这篇关于使用Closure-Compiler键入检查数组内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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