tl
tl copied to clipboard
Every type contains nil
I've noticed that this very simple example is valid according to tl
, yet it crashes at runtime:
local x: number
x = nil
print(x + 1)
From my understanding, the value nil
is considered to be contained in every type. I'm aware that some old type systems also did this, e.g. Java (for object references) and C (only for pointers). To compare, both Python and TypeScript choose a different approach here. In Python and TypeScript, there's a difference between int
and int | None
and (respectively) number
and number | null
, number | undefined
etc.
Are there any plans to change this assumption?
People do want to change it but fear of it becoming too much of a burden without syntax to make nill checks easier.
I personally rather see the split happen as well, be that with or without syntax to make null checks nicer to write.
@Feuermurmel No immediate plans to change this, but it's not entirely off the table either. There's a long thread debating on this topic over at the Discussions tab, on which I also posted my 2 cents back in April. Also related: #71.
I see. I don't have enough time to investigate this further. My gut feeling is that this is a deal-breaker of the projects I'm working on. I'm not sure whether having a nil
in a place where one shouldn't be is the number one bug occurring in production with stuff I did in Lua, but it definitely is with Java and JavaScript (probably also C# and C++, but that's a long time ago).
I stumbled over another project which adds static typing to Lua, IntelliJ-Luanalysis. It seems they don't add nil
to every type, but because of a lack of experience, I can't say how well it works out. But maybe it's worth a look.
I see. I don't have enough time to investigate this further. My gut feeling is that this is a deal-breaker of the projects I'm working on. I'm not sure whether having a
nil
in a place where one shouldn't be is the number one bug occurring in production with stuff I did in Lua, but it definitely is with Java and JavaScript (probably also C# and C++, but that's a long time ago).I stumbled over another project which adds static typing to Lua, IntelliJ-Luanalysis. It seems they don't add
nil
to every type, but because of a lack of experience, I can't say how well it works out. But maybe it's worth a look.
If you do need that type of typing (luanalysis) I recommend the lua-language-server over it, the diagnostics are better and faster
https://github.com/LuaLS/lua-language-server checks for nil
and uses the T?
syntax to denote types that can also be nil
. I strongly advocate for this. nil
errors are constant source of pain for me when using Teal, which is a pain I no longer have with normal Lua!
we should fix this issue, don't want to continue the billion dollar mistake