tl icon indicating copy to clipboard operation
tl copied to clipboard

Every type contains nil

Open Feuermurmel opened this issue 2 years ago • 6 comments

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?

Feuermurmel avatar Dec 16 '22 09:12 Feuermurmel

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.

lenscas avatar Dec 16 '22 10:12 lenscas

@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.

hishamhm avatar Dec 20 '22 19:12 hishamhm

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.

Feuermurmel avatar Dec 25 '22 11:12 Feuermurmel

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

Frityet avatar Dec 26 '22 21:12 Frityet

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!

lewis6991 avatar Feb 09 '23 10:02 lewis6991

we should fix this issue, don't want to continue the billion dollar mistake

w1kee avatar Aug 18 '23 15:08 w1kee