-
-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Top and Bottom Types Distinct from "any" #142
Comments
Definitely on my list of things to do. There is at least kind of a never type for general use, but in the case of a function it implies either ---@type string
local aString
local implicitVoid = --[[---@not string]] aString
---@type void
local explicitVoid
explicitVoid = implicitVoid |
Ah, I see. So And as you explained, |
That first case is obviously incorrect and is something I'd like to fix at some point. For the most part it comes down to the fact we don't have intersection types, as you've noted elsewhere. For that 2nd example though, what behaviour would you expect? TypeScript will allow the assignment with but I'd argue that's incorrect, or at least, less useful to developers. That said, Luanalysis can be tricked into doing something similar: ---@type string
local aString
---@type void
local explicitVoid
aString = explicitVoid
aString = math.random() and "hi" or explicitVoid; Basically if you The difference is that Luanalysis forbids assignment of "nothing" to "something" which I'd argue is more useful. When we get into set theory (e.g. unions) it disappears though as it should. |
Hey, it took a bit of time to come back to this and answer your questions. After opening the few issues on this project, I quickly decided to drop it and move to something else (TypeScriptToLua). So I'm not invested in these issues anymore, but I'll try to answer your questions.
It depends on what the intention for
With (a), allowing void to be used as a type is confusing, so With (b), the unit type should be assignable like any other type.
I've written a lot of code in langauges that treat the bottom type as a normal type (e.g. Scala) and in my experience, these types are so rare that they're never a problem. I wouldn't fret about it too much and start off with whatever the math tells you is correct. Adding more warnings/errors is always possible later (this is what TypeScript does a lot, first do it correct, and then make it more useful if the same mistakes are made repeatedly).
This makes me assume that |
On a personal note, the way this plugin treats I'd rather have a type systems that follows some mathematical reasoning, where I've built a lot of intuition from other languages, and then adds some helpful, additional safeguards on top. |
I hope I'm understanding the current intended semantics of
any
correctly. To my understanding,any
is a type that is meant to be assignable to any other type and also from any other type. It is used in places where the real type isn't known or where it would be too cumbersome to write it down. Similar toany
in TypeScript andAny
in Python.What I'm mainly missing is a Top type, i.e. a type that is assignable from any other type, but not vice-versa. This would be useful in cases where the type a type is not known and where the analyzer should check that every possible type is handled. This is what Python's
object
and TypeScript'sunknown
is.And by extension, a Bottom type could be added, i.e. a type that is assignable to any other type, but not vice-versa. This would be useful in cases that are expected to never happen, e.g. as return type of a function that never returns or that always panics. This is what Python's
Never
(alias ofNoReturn
) and TypeScript'snever
is.The text was updated successfully, but these errors were encountered: