lua-users home
lua-l archive

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

On Jun 11, 2014, at 3:29 AM, Axel Kittenberger <> wrote:

> But a switch statement in most script languages (like in Javascript) is syntactic sugar for if/else! That has always been the argument from the Lua team to why there is no switch statement in Lua. Not like for example in C where it is in fact a computed goto.

I have created a switch statement patch[1] for Lua, it uses a jump table (aka computed goto) and is considerably faster than the standard if/else structures... however, applying it to vanilla Lua does present one large issue.

Lua itself has no support for symbolic constants, thus the case values of the switch statement my patch adds have to be literal constants. This leads to the use of magic numbers which hurts overall code quality.

This, I believe, is one of the major reasons Lua does not yet have a switch statement. Without symbolic constants any switch statement added to vanilla Lua could only ever be syntactic sugar for an if/else chain.

The good news for anyone wanting to use my switch patch is that I am just finishing off a new patch which adds symbolic constant support to Lua... it would of been out earlier but I wanted to add proper vararg support and testing raised a few issues I needed to fix (yay for the test suite!).