interface inference with delegates/functions

axricard axelrwiko at gmail.com
Tue Feb 4 12:53:02 UTC 2025


Basically the same question than 
https://forum.dlang.org/post/aoltazzfmnztsyatfuft@forum.dlang.org 
but with functions/delegates and inferred return types:

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

void main()
{
     I myI = () {
     	final switch("A")
         {
         case "A": return new A();
         case "B": return new B();
         }
     }();
}
```

This won't compile, because return type "Return type of `A` 
inferred here.".

My understanding from the spec 
(https://dlang.org/spec/expression.html#lambda-return-type) is 
that it should pick `I` as return type, because it is different 
from the initial return type (`A`) and a `A` implicitly converts 
to `I`.

Is it an expected behavior ?



More information about the Digitalmars-d-learn mailing list