lua-users home
lua-l archive

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


If your point is that there are two kinds of switches (those returning a value, and those not) you're right. In C they're explicitly separate (switch..case and cond?val : cond2?:val2 ...). In my proposal, they share the same 'switch' term, but it makes little sense for the application coder to blend the two types.

Anyways, 'switch' is mainly for C/C++ comers, who don't yet feel very comfortable with lookup tables alone. And, a convenient thing for cmdline parameters s.a. below:

	while argv[i] do
        if switch( argv[i], {
            ["-width"]= do width = tonumber(argv[i+1]); i= i+1 end,
            ["-height"]= do height = tonumber(argv[i+1]); i= i+1 end,
		    ["-fullscreen"]= do videoflags= videoflags..SDL_FULLSCREEN end,
		    } ) == nil
        then    -- default
print("Usage: main_program [-width N] [-height N] [-fullscreen]\n")
	       	os.exit(1)
        end
        i= i+1
	end

You are probably right that constant values could be banned completely. But I won't do that just yet. :)

My benefits (compared to other switch alternatives I've seen) are mainly the shortened "do .. end" syntax, and the ability to take default handling completely outside of the switch structure.

-ak


10.4.2005 kello 01:57, Ben Sunshine-Hill kirjoitti:

 On Apr 9, 2005 12:48 PM, Asko Kauppi <asko.kauppi@sci.fi> wrote:
    if type(v)=="function" then
        local v2= v()  -- do we get a return value?
        if v2~=nil then

I'm a little leery of the special treatment of functions, and
specifically of functions returning values, here. Seems like it could
lead to nasty side effects... up until now, adding return values to an
existing Lua function generally hasn't had any side effects on
well-written preexisting code. I understand the increased notational
convenience, but the case of someone needing a switch statement where
some branches return constants and some execute functions is probably
rare. I suggest forcing everything to be function-like and have people
return constants from closures, or not executing anything in a switch
statement (which makes it into a simple table).

Ben