lua-users home
lua-l archive

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


2016-05-21 7:18 GMT+02:00 Egor Skriptunoff <egor.skriptunoff@gmail.com>:

> I want to share simple program that determines Lua version.
>
> local f, t = function()return function()end end, {nil,
>    [false]  = 'Lua 5.1',
>    [true]   = 'Lua 5.2',
>    [1/'-0'] = 'Lua 5.3',
>    [1]      = 'LuaJIT' }
> local version = t[1] or t[1/0] or t[f()==f()]
>
> The curious fact about this program is that it doesn't depend on anything
> that can be changed.
> It does not use any standard library function or any global variable
> (as they may be withdrawn from sandboxed environment).
> It does not rely on the name of "_ENV" upvalue
> (as it can be renamed when Lua is being built).

This program is worthy of a leading code golfer.

Just getting to understand why it works is an education by itself.