[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Desired Lua Features
- From: Sean Conner <sean@...>
- Date: Sun, 28 Jan 2018 16:59:56 -0500
It was thus said that the Great KHMan once stated:
>
> I think certain thinking processes when coding translates well to
> a switch/case kind of construct, but it doesn't happen often
> (well, depends). When it happens, the coder stops and asks, why
> can't I use switch/case here?
I'm going through my C code, looking at the various switch statments, and
a few things jump out to me. One, most of them are to handle a sequence of
values (usually an enum) that in most cases, a jump table can easily handle.
Two, state machines, whehter in parsing text or handling network protocols.
And occasionally to avoid the ugliness of an if/elseif chain.
The first case is handled well with a Lua table (I've rarely missed this
variant in Lua), and the second one I usually replace with functions using
TCO (each function handles a state) or LPeg (for the parsing tasks). That
leaves the last case, which again, can be done in Lua with a table.
Granted, there is a bit of clumsiness when it comes to using tables for
this, so I can see the desire for some syntax.
As far as fallthrough though,, that is *so* rare in my code as it's not a
consideration for me. I *have* used fallthrough, but generally it's only
when parsing the command line:
switch(argc)
{
case 3: /* we have a baz!, process and fall thorugh */
case 2: /* we have a bar!, process and fall through */
case 1: /* we have a foo!, process */
break;
default: /* we got nothing! Defaults or error, depending */
}
I'm trying to think when else I did that, and while I'm sure I *have*,
nothing comes to mind immediately.
That said, I was reminded of this article:
http://prog21.dadgum.com/166.html [1]
-spc
[1] Check the archives for there is a lot of good material here. Lots
of food for thought.