According to the specification , this is the expected behavior.
The heavy compression of the source is what happens in this case: the compiler will find all the operators that could convert A to B into both class definitions . This will require A operator B(A a) and B operator B(A a) . Then,
If such an operator does not exist or more than one such operator exists, then the conversion is ambiguous and a compile-time error occurs.
So why not give an error directly from the definition? Because both definitions are in order, but this is their use, which is why the problem arises.
Could there be a way to use conversion? I do not see an easy way to do this. I am thinking of going around the compiler by emitting IL manually. Thus, I think you can instruct the program to use one operator or another. Not sure if this is entirely possible. A tool like Reflector can help.
While there is some beauty using operator-based conversions, either one of the classes will lose one operator, or you can change ToA(A a) and FromA(A a) syntax to conversions based on a constructor. Or maybe Eric Lippert could enlighten us with some language cleverness!
source share