Proposal: Disjunction types or type expressions on generic type constraints #8739
Unanswered
fruediger
asked this question in
Language Ideas
Replies: 3 comments
-
#399? |
Beta Was this translation helpful? Give feedback.
0 replies
-
You've proposed a solution to a problem. A different, more general solution to the same problem that does not require CLR support (this one does require CLR support) is type classes. See #110 . |
Beta Was this translation helpful? Give feedback.
0 replies
-
Is the reason this would require CLR support due to the fact that the compiled dll might be referenced by a compiler that doesn’t understand what is being done? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I apologize in advance if something similar had ever been proposed before.
I would like to see something like disjunction types, at least for generic type constraints at method level.
A disjunction type thereby is a type
A | B
where a concrete instanciationt
ofA | B
could be either of typeA
or of typeB
.For type constraints this could be totally solved by using polymorphic overloading for each instanciation.
But let's have an possible example in C#:
The compiler would then turn this into something like this:
Note: The expression
a + b
is totally valid, because each of the types for which Foo got instanciated declare a+
operator.Regarding type members, special rules have to be defined. Only type members can be accessed which are in the intersection set of the involved types. That means disjuncting types leads to a conjunction of theire members. Also access to members has to be valid and meaningful in every case (for each instanciation):
Disjunction types could additionally supplemented by conjuction types. Let
A & B
be the conjunction of type. Conjunction means nothing else than, ift
is ofA & B
, thent
needs to be of typeA
while also needs to be of typeB
. Historically, this is the,
operator in C#. So&
and,
could be used synonymously.Together with disjunction types and parentheses this forms a type expression syntax.
To understand type expression on generic constraints, each type expression has to be transformed to its disjunctive normal form (DNF) with the application of the usual rules of logical expressions.
So the type expression
(A | B) & C | D
could be also expressed as(A | B), C | D
and can be transformed to its DNF(A & C) | (B & C) | D
which also could be expressed as(A, C) | (B, C) | D
. Where,
has the usual meaning of C# generic type constraints.A possible C# example:
With the rules explained above the compiler would turn this into:
However, this leads to a new source of error. A CLR type may not have two concrete base classes. But that's a usuall error when working with generics.
Would be turned to
In conclusion, type expressions do not have to be concretely represented in any way. They can be achieved completely using code transformations by the compiler. So they just need to be added to the language as syntactic sugar.
At the moment I'm not sure, if these type expressions or at least disjunctions types are also applicable to generic type constraints at type declaration level. But that would be also very nice.
Finally, I apologize for my bad language, but English is not my native language.
Beta Was this translation helpful? Give feedback.
All reactions