The Boston Diaries

The ongoing saga of a programmer who doesn't live in Boston, nor does he even like Boston, but yet named his weblog/journal “The Boston Diaries.”

Go figure.

Friday, August 14, 2015

Implementing a switch statement in Lua

From time to time, the lack of a switch statement in Lua comes up on the Lua mailing list.

Over the years I've been using Lua, I only missed switch when I first started, as I was trying to write Lua like it was C. But over time, I've come to realize that a switch statement isn't really needed that often, not with first class functions. A minimal implementation is just three lines of code:

function switch(key,case,otherwise)
  return (key[case] or otherwise or function() return nil end)(case,key)

Which allows you to do something like:

    [true]     = function() print("i is true") end,
    [10]       = function() print("i is is 10")  end,
    ['foobar'] = function() print("i is 'foobar') end,
    [print]    = function() prnit("i is the function print()") end,
  function() print("i is not something I recognize") end

It's about the same syntactical overhead as the switch statement in C or Java. The reason I pass the case table to each function is to allow a C-style “fallthrough” case, like:

    [1] = function() flag = true end,
    [2] = function(case) case[1]() print(flag) end,
  function() print "Unknown!" end

Notice how when i is 2, we execute the case for then i is 1, and print flag (also, the indices are superfluous—I just included them to be explicit). Also note how this can be used in an expression context—something that can not be said for C (or C++, or Java or … ).

Is this the best solution? Hard to say. It is the shortest, but it isn't that tolerant of of errors. But because it's so easy to implement, it can be adapted to just about any style you want.

Obligatory Picture

[“I am NOT a number, I am … a Q-CODE!”]

Obligatory Contact Info

Obligatory Feeds

Obligatory Links

Obligatory Miscellaneous

You have my permission to link freely to any entry here. Go ahead, I won't bite. I promise.

The dates are the permanent links to that day's entries (or entry, if there is only one entry). The titles are the permanent links to that entry only. The format for the links are simple: Start with the base link for this site:, then add the date you are interested in, say 2000/08/01, so that would make the final URL:

You can also specify the entire month by leaving off the day portion. You can even select an arbitrary portion of time.

You may also note subtle shading of the links and that's intentional: the “closer” the link is (relative to the page) the “brighter” it appears. It's an experiment in using color shading to denote the distance a link is from here. If you don't notice it, don't worry; it's not all that important.

It is assumed that every brand name, slogan, corporate name, symbol, design element, et cetera mentioned in these pages is a protected and/or trademarked entity, the sole property of its owner(s), and acknowledgement of this status is implied.

Copyright © 1999-2024 by Sean Conner. All Rights Reserved.