Lua has a piece of syntactic sugar that allows for OOP methods to be implemented: the operator : when used as foo:bar(biz) is equivalent to foo.bar(foo,biz). However, what I've noticed is that it is also actively used by the standard string library in this manner:
local a = "a normal string"
local b = a:reverse() -- ????????
print(b)
-- outputs: gnirts lamron a
The question is: how exactly is this achieved? Naturally, when you try to assume that it expands to a.reverse(a) it falls apart, as a is a string and cannot possibly contain the key reverse, moreover, the statement is actually equivalent to string.reverse(a). My hunch was that it actually expands to _G[type(a)].reverse(a), but then you remember that the same stuff happens in the io library on file descriptors...
a.reverse(a)actually fail? It works on tio.run: tio.run/… - aschepler