imba / docs.imba.io

📝The official Imba documentation
https://docs.imba.io
3 stars 2 forks source link

Explain `var def` #12

Open foxbunny opened 5 years ago

foxbunny commented 5 years ago

I'm admittedly unsure about why var def was introduced, but I don't see why it has to exist.

For most intents and purposes, a var def is exactly the same as assigning a do block to a variable. It does not seem to do anything more or less than that.

For example, these are pretty much identical:

var def foo x, y = 12, *z
    console.log 'called'

var foo1 = do |x, y = 12, *z| console.log 'called'

Compiled output:

function foo(x,y){
    var $0 = arguments, j = $0.length;
    if(j < 2) y = 12;
    var z = new Array(j>2 ? j-2 : 0);
    while(j>2) z[--j - 2] = $0[j];
    return console.log('called');
};

var foo1 = function(x,y) {
    var $0 = arguments, j = $0.length;
    if(j < 2) y = 12;
    var z = new Array(j>2 ? j-2 : 0);
    while(j>2) z[--j - 2] = $0[j];
    return console.log('called');
};

Compared to def, var def methods can be assigned and passed around. So can do blocks that are assigned to variables, and so can normal methods as of 1.4 where they can be referenced as self:foo. So var def seems to be redundant.