lua-users home
lua-l archive

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


I think nil has to have every type.

I disagree. Nils are often "generated" by accidentally forgetting last
function parameter(s) or return value in some of function branches.
Like this (silly) example:

 -- Returns a * b / c. Calls error if c is 0.
 local mul_div = function(a, b, c)
   if c ~= 0 then
      return (a * b) / c
   -- "Returning" nil instead of raising error. Bad implementation.

 mul_div(1, 2) -- c would be nil here, bad usage.
 -- Even worse:
 local abc = { a = 1, b = 2 } -- note no c field
 mul_div(abc.a, abc.b, abc.c) -- abc.c is nil

In my opinion, to the purpose of (runtime) type checking, "validator"
should recognize following "type kinds":


where T is a "conventional" Lua type (number, table, userdata etc.),
and optional_T is either conventional Lua type or nil (like
optional_table); any is any type except nil, and optional_any is any
type or nil.

Having nil as equivalent to any type is dangerous here.

Until we began to widely use type-validating assertions, and installed
global table protection, large percent of our bugs were due to
nil-generating behaviour shown above.

With best regards,