lua-users home
lua-l archive

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

> On Nov 20, 2013, at 1:30 PM, Sean Conner <> wrote:
>  Actually, C uses 'default', not 'else'.  That's what threw me for a loop.

Yes, it does, however, some languages do use 'else' and I thought that since I was already adding two new keywords I could simply repurpose 'else' here as it was appropriate and avoided adding a third new keyword. :)

>> It was thus said that the Great Paige DePol once stated:
>> Your examples involve the use of tables and functions, both of which are
>> unnecessary overhead that I would not want to incur for every switch
>> statement that is used. Actually, I am unsure how that would qualify as an
>> optimisation really? 
>  For space, it's not.  It's a time-optimization, as using a hash table is
> O(1) (amortized) overhead.  Also, the table/functions are only created once,
> during compilation.  For large switch statements with equal (or near equal)
> probability of among cases, I would think it's a win over a cascade of
> if/elseif statements.
>  -spc

The only reason the current version of my patch cascades is because a) it worked, b) is as efficient (or a bit more so when using a global) as existing if/elseif/else constructs and c) supporting a jump table is not possible in vanilla Lua so far as I can tell at this time! :)

Yes, you could use Lua itself to create a jump table using a Lua table and functions, but that is not a terribly nice looking construct, and I believe my jump table opcode solution would still be more efficient than a Lua based construct. Also, switch case statements are easy to understand, don't produce cluttered code and is much nicer to use than a table/function system, imo. :)