lua-users home
lua-l archive

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



On Sun, Apr 21, 2013 at 3:21 PM, Enrico Colombini <erix@erix.it> wrote:
By the way... having a default argument for a boolean:

<snip/> 
  Show(nil)   --> Show(true) -- use default
  Show(true)  --> Show(true) -- arg not nil/false, use arg
  Show(false) --> Show(true) -- unintentionally use default (oops)

--
  Enrico


This is my most hated bug. I feel like I can probably find this bug in just about every piece of lua code, even though finding it doesn't mean that the expected arguments wouldn't be processed correctly. That is, usually the caller is expected to pass a non-bool or nothing, and false would act as a lot like it's supposed to, but not always.

It always takes me too long to figure out the correct way around it, when I may want to accept many things, including a boolean, especially when I want to default to "false".

show = show ~= nil and show or false

That's fine enough, but not especially clear and requires a bit too much thinking.

-Andrew