-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Proposal: A bottom type for C# #4843
Comments
Not directly related, but what do you think about bringing Java's wildcard generics into C#? As far as I understand they could reduce the need to inherit from a non-generic base class in some cases. |
Interesting. Are there any CLR languages that implement such a feature that you know of? I'm curious as to how they may have implemented it. The issue is that the nature of generics is largely controlled by the CLR, which enforces the variance of the generic type parameters. The CLR offers nothing higher than Beyond that you have the other little issues of @dsaf Java wildcard generics don't really satisfy the problem. The wildcard only represents the lower boundary, generally |
I see that Clojure has a CLR implementation, clojure/clojure-clr. Clojure has a bottom type,
Yeah I still don't enough background to figure out if extra work on the CLR side is needed get support for this. Outside of generic type variance, how does Roslyn right now figure out that: int Foo() { throw new Exception(); } is not a type error?
Yeah, that's what I was thinking. A real problem however is what happens if someone declares a class with such a name: I'm curious what kind of keywords both make sense and can be reused. |
Duplicate of #1226. |
With the introduction of Generic covariance and contravariance in C#, it is clearer that the language would benefit from having a bottom type--a type which is a subtype of all types.
This would be similar to
undefined
in Haskell,Nothing
in Scala,!
in Rust,Benefits of a bottom-type
Collections, optional types, and type unions commonly use bottom types to make a language more expressive, but also reduce duplication/allocations, etc.
What a bottom-type could look like in C
Bottom types are usually ''unoccupied'' types, meaning that there is no valid value that represents the bottom types. Some languages have ''occupied'' bottom types (think
undefined
in JavaScript), where a value exists which satisfies the property of being a sub-type of all possible types.If there were no value types in C#, then
typeof(null)
would have been a great candidate for an occupied bottom type.We can do better. We can have a value type that is never assignable, which would make code a lot cleaner. My main proposal: the type of the expression that always throws is the bottom type.
Proposal:
undefined
/throwing
/bottom
I propose that C#'s bottom type (whatever it is called) should be the type of the expression that always throws.
I think this extends the language cleanly and makes the most sense. Check the implementation of
EmptyList
above to see how it works naturally. Accessing a value in an empty array would also always throw an out-of-range exception. Attempt to accessCurrent
of an empty enumerable will always throw. etc.This is also useful for delegates, etc.:
Non-Proposal:
void
A strawman proposal is to use
void
as the bottom type. I want to call that out as a bad option. We currently do not havevoid
declarations (compile error), and cannot use void-returning functions in expressions.The text was updated successfully, but these errors were encountered: