www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - opApply + const

reply ryuukk_ <ryuukk.dev gmail.com> writes:
Hello,

I have the following:

```D

struct Stuff {
     int opApply(scope int delegate(Stuff*) dg)
     {
         return 0;
     }

};
void main()
{
     Stuff* stuff;
     foreach(it; *stuff)
     {}
}
```

It works fine.. but when the variable becomes ``const(Stuff)* 
stuff;``

It gives me:

```
onlineapp.d(13): Error: cannot uniquely infer `foreach` argument 
types
```

I have no idea what i should be doing, does anyone have a clue?
Jan 23
next sibling parent reply Alexandru Ermicioi <alexandru.ermicioi gmail.com> writes:
On Tuesday, 23 January 2024 at 16:11:25 UTC, ryuukk_ wrote:
 Hello,

 I have the following:

 ```D

 struct Stuff {
     int opApply(scope int delegate(Stuff*) dg)
     {
         return 0;
     }

 };
 void main()
 {
     Stuff* stuff;
     foreach(it; *stuff)
     {}
 }
 ```

 It works fine.. but when the variable becomes ``const(Stuff)* 
 stuff;``

 It gives me:

 ```
 onlineapp.d(13): Error: cannot uniquely infer `foreach` 
 argument types
 ```

 I have no idea what i should be doing, does anyone have a clue?
Try making opApply const.
Jan 23
parent ryuukk_ <ryuukk.dev gmail.com> writes:
On Tuesday, 23 January 2024 at 17:07:18 UTC, Alexandru Ermicioi 
wrote:
 On Tuesday, 23 January 2024 at 16:11:25 UTC, ryuukk_ wrote:
 Hello,

 I have the following:

 ```D

 struct Stuff {
     int opApply(scope int delegate(Stuff*) dg)
     {
         return 0;
     }

 };
 void main()
 {
     Stuff* stuff;
     foreach(it; *stuff)
     {}
 }
 ```

 It works fine.. but when the variable becomes ``const(Stuff)* 
 stuff;``

 It gives me:

 ```
 onlineapp.d(13): Error: cannot uniquely infer `foreach` 
 argument types
 ```

 I have no idea what i should be doing, does anyone have a clue?
Try making opApply const.
Thanks that fixed it
Jan 23
prev sibling parent reply Paul Backus <snarwin gmail.com> writes:
On Tuesday, 23 January 2024 at 16:11:25 UTC, ryuukk_ wrote:
 It works fine.. but when the variable becomes ``const(Stuff)* 
 stuff;``

 It gives me:

 ```
 onlineapp.d(13): Error: cannot uniquely infer `foreach` 
 argument types
 ```

 I have no idea what i should be doing, does anyone have a clue?
https://issues.dlang.org/show_bug.cgi?id=24353
Jan 23
parent ryuukk_ <ryuukk.dev gmail.com> writes:
On Tuesday, 23 January 2024 at 17:22:25 UTC, Paul Backus wrote:
 On Tuesday, 23 January 2024 at 16:11:25 UTC, ryuukk_ wrote:
 It works fine.. but when the variable becomes ``const(Stuff)* 
 stuff;``

 It gives me:

 ```
 onlineapp.d(13): Error: cannot uniquely infer `foreach` 
 argument types
 ```

 I have no idea what i should be doing, does anyone have a clue?
https://issues.dlang.org/show_bug.cgi?id=24353
Thanks, the error message was confusing me.. I tried to fix it https://github.com/dlang/dmd/pull/16086
Jan 23