AS3:调用函数时没有出现类型错误

AS3: Not getting typeError when calling a function

我正在用 ActionScript 编写我自己的语言作为个人项目(是的,我想 AS3 不是构建语言的最佳语言,但没关系)。

注意:我检查了好几次,我的编译器选项'Enable Strict Mode'设置为True。我试过将它设置为 False 来尝试,但我没有得到不同的结果。

无论如何,我有一个:

package NodyCode.Classes 
{

    public class NCString 
    {

        var value:String;

        public function NCString(expression:String = "") {
            value = expression;
        }


        public function rindex(substr:NCString, startIndex:int = 0x7fffffff):uint {
            //code here
        }
    }
}

因为我在编写自己的语言,所以我需要确保函数和方法可以接受无限数量的参数。出于这个原因,我使用了一个匿名函数,以便我可以使用 apply 方法。像这样:

//This code is in a class named ClassMethods
public static var StringMethods:Object = {
    rindex: function(substr:NCString, startIndex:int = 0x7fffffff):uint {
        return this.rindex(substr, startIndex);
    }
}

并且,在我的代码的其他地方,我调用了:

return ClassMethods.StringMethods["rindex"].apply(ncstr1, [ncstr2, [5]]);

我希望在用户使用错误类型的参数时抛出错误。

因此,在这种情况下,我在 ncstr1 上调用 rindex 方法,参数为:substr = ncstr2startIndex = [5]。请注意,根据我的匿名函数的定义,startIndex 应该是一个 int 而不是 一个 Array.

所以,我预计会抛出一个错误。相反,rindex 是用 startIndex = 5.

调用的

为什么 [5] 转换为 5,我有什么办法可以防止这种情况发生吗?如果没有,我总能解决这个问题,但如果可以的话,我宁愿不这样做。

编辑:终于明白我没有提到我正在使用匿名函数。

您是否使用 strict mode set to false? (See here also 进行编译。)

strict选项:"Prints undefined property and function calls; also performs compile-time type checking on assignments and options supplied to method calls"。

它默认为 true,但如果它以某种方式设置为 false,编译时检查可能会被禁用。我会检查您的编译器设置(无论是在 IDE 中还是在命令行上编译)并确保它们是正确的。

好的,下面是评论中的内容:

我的编译器确实处于 strict 模式。我没有收到错误的原因是因为我使用了匿名函数的 apply 方法。使用 apply 方法时会放宽类型检查。这就是 [5] 被迫 5.

的原因

显然没有办法阻止这种情况。