0
votes

I'm writing my own language from ActionScript as a personal project (yeah, I guess AS3 is not the best language to build a language from, but never mind that).

NOTE: I have checked several times, and my compiler's option 'Enable Strict Mode' is set to True. I have tried setting it to False to try, but I didnt get a different result.

At any rate, I have a this:

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
        }
    }
}

Since I'm writing my own language, I need to make sure functions and methods can take un unlimited number of arguments. For this reason, I'm using an anonymous function so that I can use the apply method. Like so:

//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);
    }
}

And, somewhere else in my code, I do the call:

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

I would like an error to be thrown whenever the user uses the wrong type of argument.

So, in this case, I call the rindex method on ncstr1, with arguments: substr = ncstr2 and startIndex = [5]. Notice that, according to my anonymous function's definition, startIndex is supposed to be an int, not an Array.

So, I expected an error to be thrown. Instead, though, rindex is called with startIndex = 5.

Why is [5] converted to 5, and is there any way for me to prevent that? If there isn't, I can always work around this problem, but I'd rather not if I can do otherwise.

EDIT: Finally understood that I did not mention I was using an anonymous function.

2
'foo' doesn't seem correct either, as it's not an instance of NCString. Seems like the compiler is being funny here though, it shouldn't be happy with this at all - JKillian
Hi. Yeah, I didn't make clear that 'foo' was also a NCString instance. Changed now. - AnthonyD973
in strict mode there should be an error thrown since you pass a Array type where int is expected. In fact as it is there should even be a warning since you are not returning anything but you expect a uint return. You must have strict mode off. - BotMaster
@AnthonyD. Ahhh, ok... totally expected.. this is a quirk of AS3 and the AVM; a single element array [5] also equals an int value of 5, while an int array [5,7] has an int value of zero (0).... have to deal with this weirdness in PlayScript... - SushiHangover
@JKillian I never knew it either till I was x-compiling some AS3 Starling games via PlayScript (.Net/Mono) and had very strange issues runtime issues and backtracked it to the original code using that quirkiness... ASC creates the correct types and the ABC reflects the types correctly, but the AVM does what it does... that is what happens when you have a language with no formal language specification ;-) - SushiHangover

2 Answers

0
votes

Are you compiling with strict mode set to false? (See here also.)

The strict option: "Prints undefined property and function calls; also performs compile-time type checking on assignments and options supplied to method calls".

It defaults to true, but if it got set to false somehow, compile-time checks might be disabled. I'd check your compiler settings (whether in an IDE or if you're compiling on the command-line) and make sure they're correct.

0
votes

Okay, so here's what was said in the comments:

I did have my compiler on strict mode. The reason for which I was not getting an error is because I was using the apply method of an anonymous function. The type checks are loosened when using the apply method. That's why [5] was coerced to 5.

There is apparently no way to prevent this.