Lua — Wat?!‽
published on Thursday, February 2, 2017
Take a minute to contemplate this fine piece of lua, making use of the ... syntax for variadic functions and the builtin unpack() function:
Now what do we have here?
Right, it takes a function and some arguments and returns a callable that, when invoked, will call the function with said arguments. It's a basic version of what is called in python a partial.
Looks simple enough, but does it work?
Sure it does, lets see:
But what happens if we pass in a nil?
Okay, it gets swallowed. So, a nil terminates the argument list. Got it!
Oh, nevermind. The truth is: only a trailing nil will not be forwarded as an argument.
I meant, a trailing nil terminates the argument list at the first occurence of a nil.
What I thought, it's all consistent!
Okay, lua is much smarter than I thought. I guess, the actual rule of thumb is: a trailing nil terminates the argument list at first nil, unless its the fourth nil, then it terminates at the third. Makes sense to me!
Oh, this will be easy to integrate in the mental ruleset.
This is even easier to predict than ever anticipated. :)
Note, this feature works on lua 5.1-5.3.
For the love of all that is good and descent, if you have any sanity left, please don't use this bugged variant of bind:
It delivers completely unpredictable output such as this: