Thread overview
interface inference
Nov 23
Antonio
Nov 28
Antonio
Nov 28
Dom DiSc
Nov 28
Antonio
Nov 28
Antonio
November 23
interface I {
        bool check();
}
class A : I {
        bool check() =>true;
}
class B : I {
        bool check() =>false;
}

I aOrB(bool check) => check ? new A() : new B();

void main()
{
  assert( aOrB(true).check );
}

Compiler error:

x.d(11): Error: cannot implicitly convert expression `check ? new A : new B` of type `object.Object` to `x.I`

Basically, the ternary conditional ?: result type is not inferred even if the type returned by the two possibilities are the same.

Is it a bug or the expected behaviour?

November 28

On Thursday, 23 November 2023 at 19:17:20 UTC, Antonio wrote:

>
interface I {
        bool check();
}
class A : I {
        bool check() =>true;
}
class B : I {
        bool check() =>false;
}

I aOrB(bool check) => check ? new A() : new B();

void main()
{
  assert( aOrB(true).check );
}

Compiler error:

x.d(11): Error: cannot implicitly convert expression `check ? new A : new B` of type `object.Object` to `x.I`

I'm forced to explicitly write the cast this way

I aOrB(bool check) => check ? cast(I) new A() : cast(I) new B();

But it is not necessary when I write same code using if/else/return statements

I aOrB(bool check){
  if(check)
    return new A();
  else
    return new B();
}

Is it the expected behaviour for ternary conditional?

November 28

On Tuesday, 28 November 2023 at 11:01:14 UTC, Antonio wrote:

>
I aOrB(bool check){
  if(check)
    return new A();
  else
    return new B();
}

Is it the expected behaviour for ternary conditional?

Here the compiler knows what type to return (from the function signature).
But the ternary operator doesn't know this, so its arguments need to be of the same type (or implicitly convert to a common type).

November 28

On Tuesday, 28 November 2023 at 14:10:30 UTC, Dom DiSc wrote:

>

On Tuesday, 28 November 2023 at 11:01:14 UTC, Antonio wrote:

>
I aOrB(bool check){
  if(check)
    return new A();
  else
    return new B();
}

Is it the expected behaviour for ternary conditional?

Here the compiler knows what type to return (from the function signature).
But the ternary operator doesn't know this,

Why?... ternary operators should deduce returning type the same way

>

need to be of the same type (or implicitly convert to a common type).

It is a common type: the I interface.

In fact, if you use abstract class instead interface, it works:

abstract class I { bool check(); }
class A : I { override bool check() =>true; }
class B : I { override bool check() =>false;}

I aOrB(bool check) => check ? new A() : new B();

void main()
{
  assert( aOrB(true).check );
}

Why ternary operator accepts covariance rules applied to base class, but not applied to interface?

About cast(I)... it allows "illegal" castings causing runtime segmentation faults if not used carefully:

class X { string xname() =>"I'm x"; }
class Y { string yname() =>"I'm y"; }
void main(){
  (cast(Y) new X()).yname;  // Runtime segmentation fault
}

This is another good reason to avoid using cast when possible.

November 28

On Thursday, 23 November 2023 at 19:17:20 UTC, Antonio wrote:

>

Basically, the ternary conditional ?: result type is not inferred even if the type returned by the two possibilities are the same.

Is it a bug or the expected behaviour?

Known bug, first reported in 2009: https://issues.dlang.org/show_bug.cgi?id=3543

November 28

On Tuesday, 28 November 2023 at 15:46:18 UTC, Paul Backus wrote:

>

On Thursday, 23 November 2023 at 19:17:20 UTC, Antonio wrote:

>

Basically, the ternary conditional ?: result type is not inferred even if the type returned by the two possibilities are the same.

Is it a bug or the expected behaviour?

Known bug, first reported in 2009: https://issues.dlang.org/show_bug.cgi?id=3543

Thanks Paul (again :-) )

Oh my!!!

I wrote this question "On Thursday, 23 November 2023 at 19:17:20 UTC, Antonio wrote:"

And the original bug was Reported: 2009-11-23 03:24 UTC by nfxjfg

The same date!!!! :-)... 14 years before