"Common type" of the ternary operator expressions

Ali Çehreli acehreli at yahoo.com
Mon May 24 16:27:21 PDT 2010


"Conditional Expressions" on this page covers the ternary operator as well:

   http://digitalmars.com/d/2.0/expression.html#ConditionalExpression

It says "the second and third expressions are implicitly converted to a 
common type which becomes the result type of the conditional expression."

How "common" should the "common type" be? Wouldn't you expect the 
following ternary operator's result be I, instead of Object?

interface I {}
class A : I {}
class B : I {}

void foo(I) {}

void main()
{
     bool some_condition;
     foo(some_condition ? new A : new B);  // <-- compiler error
}

Compiler error:

Error: function deneme.foo (I _param_0) is not callable using argument 
types (Object)
Error: cannot implicitly convert expression (some_condition ? new A : 
new B) of type object.Object to deneme.I

Ali


More information about the Digitalmars-d-learn mailing list