Void is an odd bird

I’ve been jumping back into MTASC recently, and tried turning on the -strict option. Started giving me errors for all functions I had declared like this:

function foo(Void):Void
{

}

Telling me that I have an untyped parameter. I had seen something about using Void as a parameter in the comments of this post. To quote: “avoid using Void as parameter to methods. In AS2 it creates no value. The compiler simply creates an untyped parameter named Void.”

OK. I gotta find out more about this. Research let me to this post. Again, to quote: “the compiler instead declares a local variable called Void!”

A local variable called Void? How can you have a local variable with with a key word of the language as its name? Wait. Is Void a key word? A quick search of the key and reserved words list in Flash help says no. Of course, “void” (small v) is, but not its big brother, Void.

So wait a minute. That means you could do something like this:

function x(Void)
{
   trace(Void);
   trace(typeof Void);
}
x("hello");

Surely, that’s not going to work. Hmmm… it does. Damn.

OK, now I’m torn. From a code writing/reading viewpoint, Void is useful. It says, “You ought not to be passing me any arguments.” This is good to know. But if you use it, you are creating a local variable that (in any program written by a sane person) is never going to be used. Also, if you are using MTASC with -strict, you’re going to run into the issue that brought this all up for me.

I think for now, Void and I are going to be on a trial separation. Let things cool down a bit, get a clear perspective on things. I’m going to be doing some thinking… about stuff like, what the hell is Void doing here:

function foo():Void
{

}

???

This entry was posted in Flash. Bookmark the permalink.

8 Responses to Void is an odd bird

  1. Simon Wacker says:

    If you want to really omit paramaters you must write Void:Void in your parameter list. 😉 But it looks, kind of ugly. And it again creates this unnecessary parameter that slows the execution of the method a little bit down.

    Greetings,
    Simon

  2. Joseba Alonso says:

    I think that is the expected behaviour, will do the same with

    function foo(XML){
    }

    or whatever, since the class names are not reserved keywords you can create a local variable with the same name and it will obscure the one declared in _global, where all the classes live. After all, the classes are Funcion objects, and so is Void.

  3. Keith Peters says:

    “Expected” as “in the ECMA Spec” or as in “that’s obvious” ? I sure didn’t expect it.

    And I’m not sure what Void is. If it were a function object, you’d see that with trace(Void) or trace(typeof Void). Both return undefined. Yet, it has some special meaning, because you can type a function’s return value as Void, while no other random undefined variable will work there.

  4. If you had an object called b and you then had a parameter in a function called b, would you expect that b would refer to the object?

    This:
    function foo(Void):Void
    is the same as writing:
    function foo(b):Void

    Granted b is not a class in this case, but classes are just functions after all.

    As to how to make the compiler understand that the method should take no parameters, which is what I think you want, I haven’t seen anything for that.

    “If you want to really omit paramaters you must write Void:Void in your parameter list.”
    Flash gives the following error in that case: A function parameter may not be of type Void.

    Perhaps that works in MTASC …

  5. Keith Peters says:

    “This:
    function foo(Void):Void
    is the same as writing:
    function foo(b):Void”

    sure, I understand that now. I had just been assuming (along with many others, I’m sure) that Void had some special meaning like undefined or null. I thought

    Void = “hello”;
    trace(Void);

    had about as much chance of working as

    undefined = “hello”;
    trace(undefined);

    Plus, Void is not a function or a class, as two have stated so far. Tracing any function/class will trace [function Function]. Tracing typeof any class will trace function. Void traces undefined for both.

  6. Keith Peters says:

    Oops. I take it back. Void = “hello” doesn’t work.

  7. “Void traces undefined for both.”
    Ok, that’s weird … and not so, since that follows the definition of Void: it’s nothing.

    “Void = “hello” doesn’t work.”
    Actually:
    var Void = “hello”;
    works, but:
    Void = “hello”;
    doesn’t work: Type mismatch in assignment statement: found String where Void is required.

    That’s because var Void means we’re creating a variable in scope X called Void. But just saying Void falls back on _global.Void.

    … Ok, I’m with you – a little puzzled.

    Try this:

    var undefined = ‘foo’;
    trace(undefined); // returns undefined

    var undefined = ‘foo’;
    trace(this.undefined); // returns foo

    var Void = ‘bar’;
    trace(Void); // returns bar

    var Void = ‘bar’;
    trace(this.Void); // returns bar

    delete Void; // error: Classes, interfaces, and built-in types may not be deleted.

    delete this.Void; // no problem.

    So, undefined is intrinsic (truly) but Void is not. I can buy that, I guess, but it’s inconsistent, and I guess that was your point, right? Strange for sure.

  8. brainy says:

    As far as I know, Void is just there to mark a function which doesn’t return a value (or a procedure). I never saw anything mentioning that ‘Void’ in the parameter list marks the function doesn’t accept any parameters… So, as far as I can tell, that behaviour *is* expected. As Void has no meaning there, it is treated just as any other arbitrary parameter name.

Comments are closed.