Switch opcode

Hello.
Instead using cascading if… Elseif… Elseif… (and so on), would it be wise to ‘create’ a switch statement ?
To my modest point of view it could be - very - useful.
Thanks to read me.
Yours

@NialaS doing with elseif is fine for few choices. If you have many cases, you can implement the equivalent of a switch with a table of functions indexed by your cases. And this would be quite fast too. So there is pby no need to implement an other way to do this in lua.

here is what i mean

function setup()
    -- switch definition
    switch = {}
    switch["case1"] = function() print("case1") end -- of course you can do sthg else here!
    switch["case2"] = function() print("case2") end
    -- example of usage
    switch["case1"]()
end

@Jmv38 I have to disagree with you when you say “elseif is fine for a few choices”.
Here’s a modified “elseif” statement I found in another discussion. I think I would rather do the “elseif” below then try to put this in a switch table.


    if year == 2014 and month == 4 and day == 6 then
        -- do something
    elseif year == 2014 and month == 5 and day == 14 then
        -- do something
    elseif year == 2014 and month == 6 and day == 13 then
        -- do something
    elseif year == 2014 and month == 7 and day == 12 then
        -- do something
    elseif year == 2014 and month == 8 and day == 10 then
        -- do something
    elseif year == 2014 and month == 9 and day == 9 then
        -- do something
    elseif year == 2014 and month == 10 and day == 8 then
        -- do something
    elseif year == 2014 and month == 11 and day == 6 then
        -- do something
    elseif year == 2014 and month == 12 and day == 6 then
        -- do something
    end

@Dave1707 i agree with this example: it is not wel adapted to write this into a switch because there are multiple boolean tests with ‘and’. But for a ‘classical’ switch statement, on one variable value, i believe the switch-using-a-table will be faster because the hash table indexing is more efficient than multiple comparisons. Not checked, though…

You can do something like this:

function switch(st)
  return function(x) 
    return st[x] and st[x]()
  end
end

```


And use it like this:

switch{
  [1] = function() print("a") end,
  [2] = function() print("b") end,
  [3] = function() print("c") end,
}(2)

```


Note curly braces syntax for calling switch() with one table as the only argument.

To actually make this faster than if..elseif, you'd have to make sure you build the table only once, as in:

my_switch = switch{ ... }
...
my_switch(2)

```


I do think this could look more structured in cases where you have multiple tests joined with and, something like this:

my_switch = switch{
  [2014] = function() return switch{
    [1] = function() print("january") end,
    [2] = function() print("february") end
  } end
}
...
my_switch(2014)(2)

```

Better : thanks for reading me ! Sorry…

@NialaS You could create a switch statement in Lua, but it would probably be slower than using the “ifelse” statements.