interface inference with delegates/functions

axricard axelrwiko at gmail.com
Wed Feb 5 08:59:23 UTC 2025


On Wednesday, 5 February 2025 at 01:20:07 UTC, Jonathan M Davis 
wrote:
> Note that it talks about the "initial inferred return type", 
> whereas your function doesn't have an initial inferred return 
> type, because it fails to compile when determining the return 
> type. So, while the spec doesn't expand on what it's saying 
> here to give more detail, I'd say that it's definitely saying 
> that your lambda needs to compile and thus have an inferred 
> return type, and _then_ the context comes into play.
>
> And honestly, I'm pretty sure that the entire intent of this 
> portion of the spec has to do with function pointers and not 
> about converting the return type when calling the function.


Thanks for the clarification. Indeed this compiles if we separate 
function definition from its call :

``` D
  I delegate() getI = () {
     	final switch("A")
         {
         case "A": return new A();
         case "B": return new B();
         }
     };

  I myI = getI();
```


But I guess the most readable form for my case is to simply avoid 
inference :

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


More information about the Digitalmars-d-learn mailing list