-
Notifications
You must be signed in to change notification settings - Fork 62
Disjoint constraints for type parameters #7259
Comments
This is an interesting idea that we should certainly explore further. My immediate reactions:
|
One option might be something like |
i.e. Return|Error map<Value, Error, Return>
(Value|Error val, Return(Value) fun)
given Value satisfies ~Error
=> switch (val)
case (is Value) fun(val)
case (is Error) val; |
But type inference is definitely a real problem here. To see why, try out this code in current Ceylon: Return|Error map<Value, Error, Return>
(Value|Error val, Return(Value) fun)
=> switch (val)
case (is Value) fun(val)
else case (is Error) val;
String|Exception maybeString = "";
Integer int = map(maybeString, (String x)=>x.size); Ceylon does an atrocious job of inferring |
I too. (Can’t say anything about |
What about Actually, we could also write it the other way around, and reuse the existing syntax: This would allow for a future generalization like |
Inference isn't the problem here. This code is ambiguous: given different inferred types for
Now suppose you had a generic method whose type arguments are inferable. Then further requiring type parameters to be disjoint won't affect inferability so long as you're only requiring covariant type parameters to be disjoint. |
I like the Implementing this feature should make #4517 potentially easily solveable, by introducing |
I propose adding a new constraint
T1, T2, …, Tn disjoint
for signifying type parametersT1
,T2
, …,Tn
are provably disjoint at compile-time with exactly the same algorithm as used forswitch
cases.It should enable us to write e. g.
or maybe more useful
without disabling any warnings.
Maybe examples aren’t comprehensive enough, but it occurred to me this could be a nice feature and maybe even not hard to implement.
The text was updated successfully, but these errors were encountered: