Replies: 6 comments
-
This proposal differs from shapes #164, because no type parts matching performed. We are working only with solid Types. Although types union is a sort of type shape. |
Beta Was this translation helpful? Give feedback.
-
Probably this feature can be implemented as C# code. If we will have "is", "as" operators and unrestricted implicit cast operator and contravariance/invariance cast operators. Probably I will create new proposals for this ideas in the near future. |
Beta Was this translation helpful? Give feedback.
-
Actually this is reliable feature, for example this wierd trick + CLR/Compiler small change can do this: public class MyImplementation:
IDisposable,
ICollection<int>,
IMyContract.
// Implicit added by the CLR (compiler cannot do anything with already binary code).
// Should be always sorted by some stable rule
// Also should be reduced to fully orthogonal set
// (no any one interfaces are equal or inherited one from another)
__Union<IDisposable, ICollection<int>, IMyContract>
{
// Some implementation
}
// ---- Some were in runtime core library ---------
public interface __Union<out T1, out T2, out T3, out T4> :
__Union<T1, T2, T3>,
__Union<T1, T2, T4>,
__Union<T1, T3, T4>,
__Union<T2, T3, T4>
where orthogonal T1, T2, T3, T4
{
}
public interface __Union<out T1, out T2, out T3> :
__Union<T1, T2>,
__Union<T1, T3>,
__Union<T2, T3>
where orthogonal T1, T2, T3
{
}
public interface __Union<out T1, out T2> :
__Union<T1>,
__Union<T2>
where orthogonal T1, T2
{
}
public interface __Union<out T1>
{
// Default interface members feature https://github.com/dotnet/csharplang/issues/52
T1 Value
{
get
{
return (T1)this;
}
}
}
//-------------------------------------------
// union<IDisposable, ICollection<int>> is replaces by compiler with:
public void AnotherPlace1(__Union<IDisposable, ICollection<int>> myParam)
{
// And this code:
var collection = myParams as ICollection<int>;
// Transforms by compiler to:
var collection = ((__Union<ICollection<int>>)myParam).Value;
} Currently // Raises
// CS0695 '__Union<T1, T2>' cannot implement both '__Union<T1>' and '__Union<T2>' because they may unify for some type parameter substitutions
public interface __Union<out T1, out T2>: __Union<T1>, __Union<T2>
{
} Is impossible, but when we guarantee that no one T1, T2 not equal and not inherits one from another it can be allowed: // Should not raise CS0695
public interface __Union<out T1, out T2>: __Union<T1>, __Union<T2>
where orthogonal T1, T2
{
} And another problems produced by this trick should be solved in CLR, C# compiler and Reflection API. |
Beta Was this translation helpful? Give feedback.
-
Previous discussion of this concept: dotnet/roslyn#4586. |
Beta Was this translation helpful? Give feedback.
-
Thanks. Original proposal have better name. |
Beta Was this translation helpful? Give feedback.
-
Related- #344 |
Beta Was this translation helpful? Give feedback.
-
This proposal allow C# to use dynamic combination of types (mainly interfaces) as single type.
Types in C# can inherit multiple types but with those restrictions:
Let's continue to describe proposal with that in mind.
For example we have Type that inherit from 3 types.
And code where we require only two of those tree types:
Currently we needs to define additional contracts to get this behavior.
When we have large sophisticated object model, this limitation makes code very obfuscated.
Beta Was this translation helpful? Give feedback.
All reactions