[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Static typing in Lua
- From: "Alexander Gladysh" <agladysh@...>
- Date: Thu, 27 Jul 2006 21:27:48 +0400
Hi!
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
end
-- "Returning" nil instead of raising error. Bad implementation.
end
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":
nil
any
optional_any
T
optional_T
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,
Alexander.