Proposal: extend pattern matching for types #1839
Replies: 16 comments
-
You can use this instead public static int IndexOfEquals<T>(this Span<T> span, T value)
{
if (value is IEquatable<T> equatable)
{
return System.MemoryExtensions.IndexOf(span, equatable);
}
return -1;
} |
Beta Was this translation helpful? Give feedback.
-
@stepanbenes your code is incorrect because we have |
Beta Was this translation helpful? Give feedback.
-
EDIT - I noticed after posting that stepanbenes had already provided the same code example as me. I'm confused, @AlexRadch, by your "we have |
Beta Was this translation helpful? Give feedback.
-
@DavidArno your code is incorrect because we have |
Beta Was this translation helpful? Give feedback.
-
@AlexRadch then add the type constraint |
Beta Was this translation helpful? Give feedback.
-
public static int IndexOfEquals<T>(this Span<T> span, T value) where T : IEquatable<T>
{
if (value is IEquatable<T> equatable)
{
return System.MemoryExtensions.IndexOf(span, equatable);
}
return -1;
} |
Beta Was this translation helpful? Give feedback.
-
Then it will be copy of |
Beta Was this translation helpful? Give feedback.
-
If add |
Beta Was this translation helpful? Give feedback.
-
@AlexRadch Yes, you are right. I was confused by your example. You should have mentioned that you want to extend the functionality of |
Beta Was this translation helpful? Give feedback.
-
I think there are many cases where pattern matching for types will be useful. I added only one small example for Span. |
Beta Was this translation helpful? Give feedback.
-
You have posted the same code example here and in #1840 and have received the same code advice three times. This suggests that you need to improve what you are asking for as it's clearly causing confusion. Could you expand your OP please to better explain things? Also, your request for pattern matching of types makes sense but gets lost due to this confusion. |
Beta Was this translation helpful? Give feedback.
-
You are right, but I have bad English for it. I will think how to improve my question on english. |
Beta Was this translation helpful? Give feedback.
-
@DavidArno I updated my proposal. Can you correct and expand my English? |
Beta Was this translation helpful? Give feedback.
-
I don't see how your proposal would work: public static int IndexOfEquals<T>(this Span<T> span, T value)
{
if (T is IEquatable<T> Equatable)
return System.MemoryExtensions.IndexOf(span, (Equatable)value);
// something else
} would still have the problem that So I think your example wants to look something like: public static int IndexOfEquals<T>(this Span<T> span, T value)
{
if (T is IEquatable<> TEquatable &&
value is TEquatable equatableValue &&
span is Span<TEquatable> equatableSpan)
return MemoryExtensions.IndexOf(equatbleSpan, equatableValue);
// something else
} Or have I just misunderstood? |
Beta Was this translation helpful? Give feedback.
-
Mostly a duplicate of #631. Edit: this one adds the ability to introduce a constrained type parameter (type variable?) in scope at that location. |
Beta Was this translation helpful? Give feedback.
-
@DavidArno I think that next code: if (T is IEquatable<T> Equatable)
return System.MemoryExtensions.IndexOf(span, (Equatable)value); should work like next code if (T is IEquatable<T>)
return System.MemoryExtensions.IndexOf(span, (T: IEquatable<T>)value); It looks like that T support IEquatable interface. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Proposal: extend pattern matching for types.
Pattern matching for types will be useful if we want to use a method that is appropriate for our type under a certain condition, for example the method has a where constraint on some interface.
For example, we want to write the
IndexOf
method forSpan
and typeT
without limits:public static int IndexOfEquals<T>(this Span<T> span, T value)
.We can notice that if the type
T
implements the interfaceIEquatable<T>
then for suchT
corresponding method is already exists and we just need to call it, but the compiler will not allow us to do that.So I suggest to extend compiler to enable code with pattern matching for types.
Beta Was this translation helpful? Give feedback.
All reactions