6

The following operator overload is defined inside my Term class:

public static Term operator *(int c, Term t) {...}

This class also defines an implicit conversion from a Variable to Term:

public static implicit operator Term(Variable var) {...}

I would like to understand why the following does not compile:

static void Main(string[] args)
{
    Variable var = ...; // the details don't matter
    Console.WriteLine(2 * var); // var isn't implicitly converted to Term...
    Console.ReadKey();
}

The compiler says:

Operator '*' cannot be applied to operands of type 'int' and 'OOSnake.Variable'

Why isn't my overload of operator * found?

EDIT: As per the suggestion in the comments, here is a small complete example that re-produces the error:

namespace Temp
{
    class A { 
    }
    class B
    {
        public static implicit operator B(A a) { return new B(); }
        public static B operator *(int c, B b) { return new B(); }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(2 * new A());
        }
    }
}
1
  • Any chance you could write this up as a short but complete example? It makes it easier to experiment with it. (I'd also suggest not using var as a variable name, due to its other use for implicitly typed local variables...)
    – Jon Skeet
    Dec 8, 2015 at 16:59

3 Answers 3

4

Basically, operator overload resolution doesn't include implicit user-defined conversions in order to find the operators that could be applicable.

From section 7.3.4 of the C# 5 specification:

An operation of the form x op y, where op is an overloadable binary operator, x is an expression of type X, and y is an expression of type Y, is processed as follows:

  • The set of candidate user-defined operators provided by X and Y for the operation operator op(x, y) is determined. The set consists of the union of the candidate operators provided by X and the candidate operators provided by Y, each determined using the rules of §7.3.5. If X and Y are the same type, or if X and Y are derived from a common base type, then shared candidate operators only occur in the combined set once.

And 7.3.5 doesn't include implicit user-defined conversions in its search for a set of operators.

Note that this also wouldn't work if the implicit conversion to Term was declared in the Variable class - although that would be more reasonable to specify and implement, as the compiler could look at the set of conversions from the operand type to other types, and use those for overload resolution.

However, this is only a matter of looking for the operators to start with. The compiler is happy to perform implicit conversions when it considers whether or not an 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 valid:

A a = new A();        
Console.WriteLine(a * a);
5
  • I am confused. What is the difference between my example and the one you showed? Dec 8, 2015 at 17:24
  • 1
    @AlwaysLearning: In my case, the operator is declared in A, rather than in B. The compiler only looks at the operators declared in the types of the operands (and their base types) - not in the types that those operands can be converted to.
    – Jon Skeet
    Dec 8, 2015 at 17:27
  • @JonSkeet but to apply that to the original question the operator would need to be defined on the int class, correct?
    – D Stanley
    Dec 8, 2015 at 17:40
  • @DStanley: Yes - I was modifying the example as much as I needed to in order to demonstrate that implicit conversions were used for applicability but not in finding the operators to start with.
    – Jon Skeet
    Dec 8, 2015 at 17:44
  • @JonSkeet Got it. just making sure I understood. Thanks Jon.
    – D Stanley
    Dec 8, 2015 at 17:45
1

Console.WriteLine(2 * var) doesn't contain any hint that you would want to convert var to type Term. The compiler sees the int and multiplication operator and a variable of type 'Variable'.

Edit: To clarify, in order for your example to work the compiler would have to go through all the types in scope and see if one just happens to have an implicit conversion from type 'A'.

And if there also happens do be a class C like:

class C
{
    public static implicit operator C(A a) { return new A(); }
    public static B operator *(int i, C c) { return new C(); }
}

there's no telling what would happen.

Which is why the compiler doesn't do that :)

4
  • Is there a way to hint to the compiler? Dec 8, 2015 at 17:08
  • Only by explicitly casting: Console.WriteLine(2 * (Term)var);
    – Mark
    Dec 8, 2015 at 17:10
  • About the example with C. Why should the compiler give up in advance? It could declare an error only when it encounters an actual ambiguity. Dec 8, 2015 at 17:20
  • Well it's just one of the reasons not to do it. It would also take a long time because it's not just looking at your classes but all classes in all referenced assemblies. Also, the compiler would more or less be guessing what you meant which isn't really behavior you want in a compiler.
    – Mark
    Dec 8, 2015 at 17:24
0

If you want to make your example to work, you must move the implicit operator that converts A to B to the class A, like this:

namespace Temp
{
    class A
    {
        public static implicit operator B(A a) { return new B(); }
    }
    class B
    {
        public static B operator *(int c, B b) { return new B(); }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(2 * ((B)new A()));
        }
    }
}

But you cant use this 2 * new A() because(Jon Skeet answer):

Basically, operator overload resolution doesn't include implicit user-defined conversions in order to find the operators that could be applicable.

https://stackoverflow.com/a/34161798/815590

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.