On Sunday, 30 August 2026 at 03:28:49 UTC, Valentino Giudice wrote:

int opApply(scope int delegate(MyClass item) dg)

int opApply(scope int delegate(const MyClass item) dg) const

I dont think this does what you think it does; I dont use keywords that can only make less code work but im pretty sure that const does nothing, the way youve written it is confusing yourself, its applied to the int and futhermore opApplies return is a return code for asm gotos

```d
import std;
struct bar{
        const int i;
        int j;
        const int front()=>i+j;
        void popFront(){j++;}
        bool empty()=>j>10;
}
unittest{
        foreach(const i;bar(2)){
                typeof(i).stringof.writeln;
        }
        foreach(i;bar(4)){
                typeof(i).stringof.writeln;
        }
}
```

the constness of foreach varibles is part of a constructed for loop; d avoid bidirectional type propganging like c++ or haskell (because those languages are slow and buggy when you use it)

---

```d
import std;
class MyClass{}
struct bar{
        int opApply(scope int delegate(MyClass item) dg)
        {
        for (size_t i = 0; i < 2; i++)
        {
                int result = dg(new MyClass());
                if (result != 0)
                {
                        return result;
                }
        }
        return 0;
        }
}
unittest{
        foreach(const i;bar()){
                typeof(i).stringof.writeln;
        }
        foreach(i;bar()){
                typeof(i).stringof.writeln;
        }
}
```

```
const(MyClass)
const(MyClass)
MyClass
MyClass
```

you should probably not use opApply if your trying to be safe (opApplys uses are hacks like hotloading and injecting long distance jumps) its just part of foreach, and foreach makes copies anyway unless you say "ref"

Reply via email to