[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [PATCH] setmetatable({}) causes error
- From: Nathaniel Musgrove <proton.zero@...>
- Date: Thu, 13 Jul 2017 08:22:54 -0700
On Thu, Jul 13, 2017 at 6:16 AM, Luiz Henrique de Figueiredo
<lhf@tecgraf.puc-rio.br> wrote:
>> There are exactly zero other scenarios in Lua where f(nil) produces different
>> behavior than f().
>
> Try assert(), getmetatable(), ipairs(), pcall(), tonumber(), tostring(), type().
> select('#')
The difference is that those are degenerate scenarios. There is no reason that
anyone would ever actually write those function calls, because the outcome is
easily predicted, and more importantly useless.
By comparison, setmetatable(t) actually performs a task, one that a programmer
would be entirely reasonable in wanting to perform.
At the end of the day, you say it might be confusing to omit a nil, but at
least when someone says "why doesn't setmetatable(t) raise an error?" I can
explain that omitting an argument is equivalent to explicitly passing nil
(which is a very basic, consistent Lua principle), and passing nil to
setmetatable() has explicitly-defined behavior.
What's confusing is beating your head against a wall for half an hour trying
to figure out why on Earth a function call is raising an error before finally
finding the answer in Lua's _source code_. That was time out of my life that
had no reason at all to be spent, and I would rather that no one else ever
have to do that.
Regards,
Nathaniel