Replies: 15 comments
-
Tagging @gafter FYI |
Beta Was this translation helpful? Give feedback.
-
I'm tentatively 👍'ing this as I've run into issues with wanting patterns to handle implicit conversions. It's only tentative though as my gut feeling is that user-defined positional/active patterns might cover these scenarios, possibly making this unnecessary. |
Beta Was this translation helpful? Give feedback.
-
This would a bit undermine the similar behavior we intend to get for dynamic vs static checks. For example: HasConversionToInt x = new HasConversionToInt(3);
if (x is int z) // succeeds?
object y = x;
if (y is int z) // fails? |
Beta Was this translation helpful? Give feedback.
-
I find this useful, but potentially troublesome as @gafter outlined above. @DavidArno How would you use active patterns for type conversion? |
Beta Was this translation helpful? Give feedback.
-
According to @HaloFour, it doesn't. if (y is int z) // fails? I think |
Beta Was this translation helpful? Give feedback.
-
The exact design of that is still up in the air. It's possible that they might if you can write them as extension static methods and the extended type doesn't have to be static. Maybe something like this: public extension IntPattern of int {
public static bool Deconstruct(ReadOnly<int> ro, out int value) { ... }
} But I can't see how that would work on
I believe he's referring to pattern matching against an expression of a different type, like object o = new ReadOnly<int>(2);
if (o is int i) { ... } The problem is that the compiler has no idea that |
Beta Was this translation helpful? Give feedback.
-
I think it shouldn't and that's not a problem.
I wonder what's the reason for a name here lol |
Beta Was this translation helpful? Give feedback.
-
Well, given @gafter 's comment, it sounds like maybe he thinks that it should.
Extension members still have to exist in their own type, just like extension methods do today. Since that type would be a part of a public surface it's safer to have the developer explicitly define the name rather than have the compiler generate one. |
Beta Was this translation helpful? Give feedback.
-
That’s a CLR limitation? |
Beta Was this translation helpful? Give feedback.
-
Ha, interpretation is in the mind of the reader, or some such. What @HaloFour shows in that comment is exactly what I was going to use as an example of how a user-defined pattern could handle such scenarios 😀 |
Beta Was this translation helpful? Give feedback.
-
I wish it should be a new keyword for convertible I don't against the feature, its great. But we should leave Suppose object a = new SomeClass();
if(a is SomeClass sc)
sc.SomeProperty = 15; // a would be set property to 15
if(a is ImplicitConversionToSomeClass ic)
ic.SomeProperty = 15; // if we allow implicit conversion here it would create new object to set value So we should have keyword |
Beta Was this translation helpful? Give feedback.
-
IIRC, this is already what happens with primitives and structs. |
Beta Was this translation helpful? Give feedback.
-
@yaakov-h That's unavoidable but at least it common in C# for struct and class to have difference behavior. This proposal make class have an unpredictable behavior. Means it might or might not set the value to the original object And if someone already have code like this it would be breaking change |
Beta Was this translation helpful? Give feedback.
-
How about, and I'm only half serious on this: if(a is ImplicitConversionToSomeClass ic)
ic.SomeProperty = 15; // we allow implicit conversion here which does what it wants, including potentially creating a new object
if(a is really ImplicitConversionToSomeClass ic)
ic.SomeProperty = 15; // implicit conversion cannot reach here Or, the inverse, if(a is ImplicitConversionToSomeClass ic)
ic.SomeProperty = 15; // implicit conversion cannot reach here
if(a is kinda ImplicitConversionToSomeClass ic)
ic.SomeProperty = 15; // we allow implicit conversion here which does what it wants, including potentially creating a new object |
Beta Was this translation helpful? Give feedback.
-
@yaakov-h It too messy to append more keyword after I think if(a maybe ImplicitConversionToSomeClass ic)
ic.SomeProperty = 15; // we allow implicit conversion here which does what it wants, including potentially creating a new object better |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
@Logerfo commented on Tue Jan 09 2018
Context
Recently, I've opened this stackoverflow question, which brought me to #1047 (comment).
The idea is to apply implicit operators when matching unhandleable patterns (I really hope this word does in fact exist).
Scenario
So, considering the following struct:
We get:
Suggestion
When a pattern cannot be handled, the compiler should apply the implicit operator which allows the expression to be resolved, if any. If there are multiple possible implicit operators that would cover the issue, nothing should be done and the compilation error must prevail.
This is a suggestion, not a proposal, which means that no detail is final. Feel free to suggest changes or demonstrate barriers.
Why?
The compiler has some hard code for the
Nullable<T>
to work in this scenario. Implementing this suggestion would allow the struct to have aimplicit operator T
. If so, the hard code would not be necessary anymore, and users could achieve the same behavior with any type.Alternative
One can just use a explicit conversion operator (cast) in order to achieve the expression resolution.
Unanswered questions
Edit1: added "Why?" and "Alternative" sections.
@jcouv commented on Wed Jan 10 2018
This is a language design discussion. I'll move to csharplang. Thanks
Beta Was this translation helpful? Give feedback.
All reactions