lua-users home
lua-l archive

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



Being able to escape the whole switch construct (they are often big) with a simple 'break' is often used (in C) and handy.

I wouldn't use return for that. A return within a case would simply return from the whole function as usual.

match m(k) do
  => 10 or 11 then  ...block...
  => 12 then        	 ...block...
  => 13, 16 then    ...block...
  => else           	...block...
end

The => or 'with' is surface, don't stick on that.

'm' is a generator for a closure, tying the reasoning code and the match value ('k', or multiple values) to itself. Returns bool function ( ... ), which is used for testing a match.

Note that "13, 16" would here mean two values fed to the match function. This leaves an awful lot of power on just how the match closure wants to deal with them; either matching them all at once (13 or 16) or using them as a range. I feel this kind of behaviour is strongly alike the 'pairs', 'ipairs' mechanism, and therefore fits nicely in existing Lua world.

Generated code would be:

do
    local mk= m(k)
    if mk(10) or mk(11) then repeat ...block... until false
    elseif mk( 12 ) then repeat ...block... until false
    elseif mk( 13,16 ) then repeat ...block... until false
    else repeat ...block... until false
    end
end

Now, we might want to deal with the basic match (numbers, strings, etc.) in a relaxed fashion, allowing them to be used directly instead of through m().

match val do
	=> 10 or 11 then
	=> 12 then
	=> 13 or 14 or 15 or 16 then
	=> else
end

The generated code would check type of value given to it, and use a simple premade m_basic() if the value's not a function. In fact, can we somehow tag function "interfaces" so that it'd be able to know whether a function is a _match comparison_ function. Anyways...

do
    local mk= val
    if type(val) ~= "function" or !is_match_comparison(val) then
	mk= m_basic(val)
    end
    if mk(10) or mk(11) then repeat ...block... until false
    elseif mk( 12 ) then repeat ...block... until false
    elseif mk( 13,16 ) then repeat ...block... until false
    else repeat ...block... until false
    end
end

After writing all this, I wonder if it's worth it. The mechanism seems valid though and maybe someone gets some ideas from this. Or just gives a looooong siiiigh.

What this would need (lacks) is ability to jump between the cases. A special case being carrying on from earlier one to the next. Don't blame me C-ish for asking such, it _is_ usable in GUI callback handling at least. Tail recursion and the traditional way of dealing with this (functions, not ifelse) may take the win, here.

;)

-asko


steve donovan kirjoitti 9.11.2007 kello 11:41:

On Nov 9, 2007 11:05 AM, Asko Kauppi <askok@dnainternet.net> wrote:
How about making a "competition" or a quest for the perfect switch
(if there is one) right here on the mailing list?

I'm wondering, was there ever a great language feature which came out
of a mailing list discussion? Not sarcastic, just genuinely
interested.

My vote is for David Given's suggestion:

match k
  with 10, 11 then return 1 end
  with 12 then return 2 end
  with i if 13<=i and i<=16 then return 4 end
end

It's understood to involve just linear chains of elseifs ;)

steve d.