lua-users home
lua-l archive

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


Op Ma., 3 Sep. 2018 om 22:03 het Andrew Gierth
<andrew@tao11.riddles.org.uk> geskryf:

> Some of the base functions are essentially part of the language -
> especially select(), pairs(), ipairs(), type(), pcall(), error(),
> assert(), tonumber(), tostring(). Without at least those, writing any
> nontrivial code will be hard.

In fact, without tostring, print() does not work. (Monkey-patching
print by changing tostring is an amusing pastime.)

I would add the string library to "essentially part of the language",
but you can't hide that without invoking debug.setmetatable.

Three of the items on the list are basically dispensible, though.

select('#',...) can't be done in another way, but select(n,...) is
equivalent to {...}[n] (slower, of course, but a sandbox is ... well,
filled with sand, so not the ideal place for running fast).

pairs(tbl) is equivalent to next,tbl which uses one keystroke less,
except that pairs honours the __pairs metamethod.

"for k,v in ipairs(tbl) do" is equivalent to "for k=1,1e400 do local
v=tbl[k] if v==nil then break end" which is much longer.

If you allow me to keep string metamethods, tonumber can be avoided too.