Replies: 16 comments
-
Let's throw in some additional ideas:
|
Beta Was this translation helpful? Give feedback.
-
How about This would then allow expressions such as |
Beta Was this translation helpful? Give feedback.
-
@YaakovDavis Those are interesting. I'm not entirely sure if there would be a fundamental difference between
|
Beta Was this translation helpful? Give feedback.
-
@yaakov-h I think detecting interface implementation is compelling. Though, I believe |
Beta Was this translation helpful? Give feedback.
-
@bondsbw
is equivalent to:
Similarly, the following:
is equivalent to:
(The actual translation is even more complex, as we need to consider also types implementing |
Beta Was this translation helpful? Give feedback.
-
@bondsbw So given:
Should we say this is a valid case, or not? My initial reaction is "yeah, why not". |
Beta Was this translation helpful? Give feedback.
-
Because an if (x as int? y) { ... } |
Beta Was this translation helpful? Give feedback.
-
I can't say I have a firm opinion. Though I'm not sure that |
Beta Was this translation helpful? Give feedback.
-
And actually, I didn't realize until I just tried it, that your code doesn't compile with C# 7. Nullable types can't be used in type patterns? It's due to CS8116. The reverse is legal: int? x = 4;
if (x is int y) { ... } That might play into how this proposal should work, I'm not sure. |
Beta Was this translation helpful? Give feedback.
-
@bondsbw what do you mean with "doesn't compile"? Yes it does. And as far as I know, |
Beta Was this translation helpful? Give feedback.
-
@Logerfo That does compile, but (And to be clear, the CS8116 is specifically for the expanded version |
Beta Was this translation helpful? Give feedback.
-
@bondsbw Oh, that's confusing. My bad, thanks for the clarification! |
Beta Was this translation helpful? Give feedback.
-
And also types implementing
All these ideas are amazing nice-to-haves which I've long felt were lacking in the language, and yet by the same token I can't imagine the LDT prioritizing them over other new features. |
Beta Was this translation helpful? Give feedback.
-
In my opinion the alternate, where subclasses and interfaces would match, would make more sense. Check out the extensions to reflection that I've submitted to CoreFX here: https://github.com/dotnet/corefx/issues/644 if (obj != null && obj.GetType().ImplementsInterface(typeof(IDictionary<,>), out var keyType, out var itemType)) {
// do stuff here
} It makes reflection with generics so much easier. And please weigh in because the BCL team seem to not understand how ridiculously awful the reflection situation has been since the introduction of generics. I'd not be at all opposed to such matching existing in the language as well, but the compiler would be forced to output some nasty boilerplate code. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour What does your ImplementsInterface do for multiple implementations, throw? I added generic type parameter inference to AutoMapper a year ago so that
Yes, they designed the type system, better them than me maintaining that. 😁 |
Beta Was this translation helpful? Give feedback.
-
Wasn't a situation I specifically guarded against. The implementation I provided in the gist attached to that issue would find one of the implementations and return
Well, a different team, but they're certainly closer to each other than we are to either of them. And even if CoreFX and eventually BCL adopted better reflection APIs the compiler likely couldn't take advantage of it if there would be any expectation of targeting older frameworks. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Allow simplified pattern matching syntax to compare types.
Given types
T
andU
(either of which may be a generic type parameter), allow the expressionwhich would expand to
Example
which is equivalent to
Alternate consideration
Allow derived types to meet the condition. The original expression would then expand to
So
Beta Was this translation helpful? Give feedback.
All reactions