Replies: 5 comments 14 replies
-
Nit: I think you mean array or |
Beta Was this translation helpful? Give feedback.
-
😂 The last few LDM (notes) have all been quite exciting to see, and quite interesting. |
Beta Was this translation helpful? Give feedback.
-
I haven't thought through this too too much, so don't consider this to be a proposal, but with the disclaimer, why don't we do this? struct OneOf<T1, T2>
{
// new type of operator
public static check operator bool(OneOf<T1, T2> operand, out T1? result)
{
if(operand.HasFirst) { result = operand.First; return true; }
return false;
}
public static check operator bool(OneOf<T1, T2> operand, out T2? result)
{
if(operand.HasSecond) { result = operand.Second; return true; }
return false;
}
// ....
}
OneOf<int, string> x = 123;
if(x is int i) { ... }
if(x is string s) { ... }
// additional scenarios allowed by the new "check" operator
implicit extension for String
{
public static check operator bool(string operand, out int result) => int.TryParse(operand, out result);
}
string str = "123";
if(str is int i) { ... }
explicit extension NumberString for String
{
public int CompareTo(NumberString other) { ... }
public static check operator bool(string operand, out NumberString result) { ... }
}
string str = "123";
if(str is NumberString numStr) { ... } Basically, the "one of many existing types" feature is just a new type in the BCL, and it uses a new type of operator which allows |
Beta Was this translation helpful? Give feedback.
-
I think the question has to be "how can we make that work". For
If I have a tag union, if (x is Shape.Rectangle) ... or I can extract its details if it is: if (x is Shape.Rectangle { Length: var length, Height: var height }) ...` It doesn't seem unreasonable to insist that if I want to capture the |
Beta Was this translation helpful? Give feedback.
-
What other past features can take advantage of this? The proposal thread mentions discards, Also, features like #946 can definitely get simpler if some level of break is allowed. Would something like "covariant return types with implicit interface implementations" be considered too (as long as a fix-all could be implemented to adjust affected code)? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
https://github.com/dotnet/csharplang/blob/main/meetings/2023/LDM-2023-03-08.md
Agenda
Beta Was this translation helpful? Give feedback.
All reactions