digitalmars.D.learn - interface inference
- Antonio (25/25) Nov 23 2023 ```d
- Antonio (16/37) Nov 28 2023 I'm forced to explicitly write the cast this way
- Dom DiSc (5/14) Nov 28 2023 Here the compiler knows what type to return (from the function
- Antonio (28/44) Nov 28 2023 Why?... ternary operators should deduce returning type the same
- Paul Backus (3/7) Nov 28 2023 Known bug, first reported in 2009:
- Antonio (8/16) Nov 28 2023 Thanks Paul (again :-) )
```d 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: ```d 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?**
Nov 23 2023
On Thursday, 23 November 2023 at 19:17:20 UTC, Antonio wrote:```d 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: ```d 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 ```d 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 ```d I aOrB(bool check){ if(check) return new A(); else return new B(); } ``` **Is it the expected behaviour for ternary conditional?**
Nov 28 2023
On Tuesday, 28 November 2023 at 11:01:14 UTC, Antonio wrote:```d 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).
Nov 28 2023
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:Why?... ternary operators should deduce returning type the same way```d 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,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: ```d 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: ```d 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.
Nov 28 2023
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
Nov 28 2023
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: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***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
Nov 28 2023