lua-users home
lua-l archive

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


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello all,

10 programmers result in 20 solution... That's my suggestion:

	function switch(actual)
	  local found = false
	  local case = function(expected)
	    if expected == actual then
	      found = true
	      return true
	    end
	  end
	  local default = function()
	    return not found
	  end
	  return case, default
	end

Usage:

	for i = 1, 4 do
	  local case, default = switch(i)

	  if case(2) then
	    print("two")
	  end

	  if case(3) then
	    print("three")
	  end

	  if default() then
	    print("default:", i)
	  end
	end


You see, you could do all crazy things with this switch implementation...


Michael Roth

-----BEGIN PGP SIGNATURE-----
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFBNgaFSIrOxc3jOmoRAmHMAJwPNGAltbVRJAypdRuE0fmjpR1OBgCfcfuL
37eJ8hQpGyMlgzlopMhWzfw=
=ma8d
-----END PGP SIGNATURE-----