lua-users home
lua-l archive

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

Dirk Laurie <> wrote:

> 2018-01-28 10:52 GMT+02:00 KHMan <>:
>> I'd like a native switch. :-) Please, pretty pretty please :-)
> So which do you want? Like Pascal's 'case' statement [1]
> or like C's 'switch' statement?
> Personally I heartily detest the C 'switch'. You need to put
> 'break' all over the place. I never use the fall-through trick.
> It's a fast-track to lurking bugs.

This is why my patch offers both options, when you declare the switch
you can choose to break or fallthrough as the default action.

The other issue with non-native switch cases that rely on functions is
the overhead of the function call as well, plus the lack of fallthrough
should that be required.

My patch does still require the overhead of a table lookup, however,
I can't really see any way around that requirement as the case values
have to be stored in some sort of lookup table.

I think another issue with switch/case in Lua is that vanilla Lua has no
facility for const values that aren't just table lookups or variables,
neither of which can be used as case values for a switch statement... so
you're left with coding potential "magic numbers" as case values, which
isn't good... and is also a limitation present in my patch as well.

For Lua++ I have a pretty robust preprocessor along with an additional
patch which allows for the declaration of consts, enums, macros, and
even basic inline functions. This patch and my switch/case patch work
quite well together at eliminating magic numbers.

One thing I have figured out and will be implementing is the ability for
macros to combine tokens and parameters that can then be fed back through
the parser. Right now my macros are essentially just recorded token streams
with parameters that can be replaced, but new tokens can not yet be formed
and re-parsed as part of the macro stream.