-
Not sure why CS is complaining about ambiguity in this case: public readonly struct OneOf<T0, T1> {
internal readonly T0? _value0;
internal readonly T1? _value1;
}
public static class Extensions {
//Open Generic deconstructor
[OverloadResolutionPriority(-10)]
public static void Deconstruct<T0, T1>(this OneOf<T0, T1> it, out T0? v0, out T1? v1) { v0 = it._value0; v1 = it._value1; }
}
public static class Extensions2 {
//Closed Generic deconstructor
[OverloadResolutionPriority(1)]
public static void Deconstruct(this OneOf<int, char> v, out int i1, out char c2) {
//...
}
}
static class ConsumeAPI {
static void Foo(OneOf<int, char> v) {
var (i, c) = v; // <- CS0121: The call is ambiguous between the following methods or properties: 'Extensions.Deconstruct<T0, T1>(OneOf<T0, T1>, out T0?, out T1?)' and 'Extensions2.Deconstruct(OneOf<int, char>, out int, out char)'
}
} I would imagine that even without ORP attribute, the one with concrete types (closed generic - |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 10 replies
-
Certainly, the ORPA behavior is by design. The spec explicitly says that ORPA is only considered within the containing type of the method, and that it cannot be used for conflicting extension method disambiguation when those extensions are defined in different types. As to the overall question, I'm not entirely certain that deconstruction uses overload resolution by the same rules as standard method overload resolution, but I'd need to read through the spec and play with the code a bit to answer whether this is a bug. Perhaps @jcouv has some insight? |
Beta Was this translation helpful? Give feedback.
-
This works for me in VS 2022 17.13.6 as well as in Sharplab.io. So I think there might be something else going on. Ah, I figured it out. If you make the But then if you make the type parameters for the Also, if you make everything, including the fields in If you need that mixing of nullable and non-nullable then it's probably worth rethinking what you are doing. I would think that would cause a lot of problems. |
Beta Was this translation helpful? Give feedback.
To be specific, I verified in
main
branch that ORPA works as expected with extensionDeconstruct
methods, and also that genericDeconstruct
methods work as expected.I also manually tested your code above in a recent VS preview (using compiler version: '4.14.0-3.25227.1 (49152f06)') and got not compilation error.