closures + struct: Error: forward reference to inferred return type of function call

ddcovery antoniocabreraperez at gmail.com
Tue Dec 15 23:14:28 UTC 2020


On Tuesday, 15 December 2020 at 19:53:33 UTC, Q. Schroll wrote:
> On Monday, 14 December 2020 at 14:39:14 UTC, ddcovery wrote:
>> On Monday, 14 December 2020 at 12:22:26 UTC, ddcovery wrote:
>>>     int opCmp(Number other){
>>>       return _value - other.value;
>>>     };
>> Correction:
>>
>> bool opEquals(Number other){
>>   return _value == other.value;
>> };
>
> You could just give the struct a variable and use that instead 
> of the enclosing function's parameter:
>
> https://run.dlang.io/is/4Lqf15
>
> Then, the struct can actually be static.

First of all, thanks for maintaining opCmp... reading 
documentation I thought that == and != needed a opEquals 
method... but opCmp is enough.

Your solution is really nice for the example, but at the cost of 
eliminating the closure (  In fact, the function is not necessary 
either: the structure itself is sufficient for the example).


As I understand, problem is that calling "number" function from 
struct is not possible because "number" should return an struct 
definition that doesn't exist (because it's definition is not 
finished yet).

It is similar to the problem with recursive lambda call using an 
"proxy" variable:

const factorial = (int n) => n==0 ? 1 : n * factorial(n-1);

*factorial* is not defined yet when compiler analyzes lambda 
expression.

This is something to keep in mind for people coming from 
python/javascript languajes.  Sometimes I forget D is not an 
scripting/dynamic one (note:  crystal has the same limitation... 
it says "Error: can't use variable name 'factorial' inside 
assignment to variable 'factorial'").




More information about the Digitalmars-d-learn mailing list