[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Have you noticed how nil is a separate type?
- From: "Soni L." <fakedme@...>
- Date: Mon, 20 Feb 2017 20:36:32 -0300
Have you noticed how nil is a separate type, and any form of
typechecking...:
local function f(x)
assert(type(x) == "table")
end
... is non-nil by default?
assert(pcall(f, nil) == false)
Of course, lua can only typecheck at runtime. But here's an example:
local function f(x)
--assert(type(x) == "function")
return {dothing=x}
end
local v = f(nil)
v:dothing()
When running this code, you get an error on line "v:dothing()". If you
uncomment the assertion, you get an error on line "local v = f(nil)"
instead, which is much more helpful.
This is why I made typecheck[1].
[1] http://marc.info/?l=lua-l&m=148755896416687&w=2
Side-note: For some reason most static type checking (e.g. C) is
nullable-by-default, while most dynamic/runtime type checking (e.g. Lua)
is non-null-by-default. And Java has both (`null instanceof Something`
is always false, yet you can pass `null` to a method expecting a
`Something` without any warnings).
--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.