lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


The problem with that version is that it builds the switch table every time.

Hence, you want to build the switch table out of line. That gets a bit more
cryptic and doesn't do the right thing if you need access to local variables
in the switch cases.

I used to think switch wasn't needed. I may have just convinced myself
otherwise -- though obviously the appropriate effect can be achieved with an
if statement.

This also points to one optimization I've been thinking it might be useful
for the compiler to make: If an inner function definition doesn't need to be
nested, then it could be promoted out to an outer scope and referenced as an
upvalue thereby allowing it to be created only once rather than each time
through the outer function. Where this breaks, however, is with respect to
function environments. Hence, it might only work if the inner function also
referenced no globals. Example:

    function outer()
        pcall( function( arg1, arg2 ) "Do something" end, param1, param2 )
    end

Could be translated to:

    local function inner( arg1, arg2 ) "Do something" end
        -- Create the function once

    function outer()
        pcall( inner, param1, param2 )
    end

Maybe the compiler already does this?

Or this could be another case of wanting lightweight, stack-allocated values
that get turned into heavier values if they escape. See the recent tuple
discussion.

Mark

on 9/1/04 8:47 AM, Warren Merrifield at wmerrifield@gmail.com wrote:

> True, that does look very cryptic, but if you wrap it all up with real
> names, I think that it's not too odd, really. And learning to think
> like this is a key to using Lua well, IMHO. I've used the foo["bar"]
> syntax here, rather than foo.bar, as I think it's clearer in this
> instance.
> 
> -- Example switch. I've put it inside it's own function for clarity.
> function doswitch(case)
>   -- I'll assert here, but you could default by calling another
>   -- function if the case argument is nil.
>   assert(type(case)=="string");
> 
>   local switch = {};
>   switch["attack"] = function()
>       -- Either write the attack behaviour in here, or refer to
>       -- an external function library here.
>   end
> 
>   switch["defend"] = function()
>       -- Either write the defend behaviour in here, or refer to
>       -- an external function library here.
>   end
> 
>   return switch[case]();
> end
> 
> -- Example usage
> state = "attack";
> action = doswitch(state);
> 
> On Wed, 1 Sep 2004 18:38:54 +0300, Asko Kauppi <asko.kauppi@sci.fi> wrote:
>> 
>> Face it, that looks really cryptical for the newcomer.
>> 
>> Personally, I would welcome select/case/default into the language, but
>> then again, using lookups (in other ways than what you describe here)
>> generally diminishes the need itself for a switch statement.  But it
>> wouldn't hurt, either.
>> 
>> -ak
>> 
>> 1.9.2004 kello 18:00, David Given kirjoitti:
>> 
>> 
>> 
>>>  I may be being a killjoy, but:
>>> 
>>> ({
>>>       [0] = function ()
>>>               print(0)
>>>       end,
>>> 
>>>       [1] = function ()
>>>               print(1)
>>>       end
>>> }[value] or function ()
>>>       print("default")
>>> end)()
>> 
>>