teal-language / tl

The compiler for Teal, a typed dialect of Lua
MIT License
2.04k stars 101 forks source link

Every type contains nil #598

Open Feuermurmel opened 1 year ago

Feuermurmel commented 1 year ago

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?

lenscas commented 1 year ago

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.

hishamhm commented 1 year ago

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

Feuermurmel commented 1 year ago

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.

Frityet commented 1 year ago

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

lewis6991 commented 1 year ago

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!

w1kee commented 11 months ago

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