In principle, allowing operator overloading does not include implicit user conversions to find operators that may be applicable.
From section 7.3.4 of the C # 5 specification:
An operation of the form x op y , where op is an overloaded binary operator, x is an expression of type x , and y is an expression of type y , is processed as follows:
- The set of user-defined operators provided by
x and y for the op(x, y) operator is defined. The set consists of a combination of candidate operators provided by X and candidate operators provided by y , each of which is determined using the rules of ยง7.3.5. If x and y are of the same type, or if x and y derived from a common base type, then common candidate operators occur only in the combined set once.
And 7.3.5 does not include implicit user conversions when looking for a set of statements.
Please note that this also will not work if the implicit conversion to Term was declared in the Variable class, although it would be more reasonable to specify and implement it, since the compiler could look at the set of transformations from the operand type for other types and use it to resolve overloading.
However, this is only a matter of finding operators. The compiler gladly performs implicit conversions when it considers whether overload is applicable. For example, in your case, if you add:
class A { public static B operator *(A a, B b) { return new B(); } }
Then this is true:
A a = new A(); Console.WriteLine(a * a);
source share