How do I _really_ implement opApply?

WebFreak001 d.forum at webfreak.org
Wed Nov 30 00:50:46 UTC 2022


it seems now when trying to cover scope semantics, @safe/@system 
and pure it already becomes quite unmanagable to implement 
opApply properly.

Right now this is my solution:

```d
private static enum opApplyImpl = q{
    int result;
    foreach (string key, ref value; this.table) {
       result = dg(key, value);
       if (result) {
          break;
       }
    }
    return result;
};

public int opApply(scope int delegate(string,       ref       
TOMLValue) @safe   dg)      @safe              { 
mixin(opApplyImpl); }
public int opApply(scope int delegate(string,       ref const 
TOMLValue) @safe   dg)      @safe              { 
mixin(opApplyImpl); }
public int opApply(scope int delegate(string, scope ref       
TOMLValue) @safe   dg)      @safe              { 
mixin(opApplyImpl); }
public int opApply(scope int delegate(string, scope ref const 
TOMLValue) @safe   dg)      @safe              { 
mixin(opApplyImpl); }
public int opApply(scope int delegate(string,       ref const 
TOMLValue) @safe   dg)      @safe const        { 
mixin(opApplyImpl); }
public int opApply(scope int delegate(string, scope ref const 
TOMLValue) @safe   dg)      @safe const        { 
mixin(opApplyImpl); }
public int opApply(scope int delegate(string,       ref       
TOMLValue) @safe   pure dg) @safe pure         { 
mixin(opApplyImpl); }
public int opApply(scope int delegate(string,       ref const 
TOMLValue) @safe   pure dg) @safe pure         { 
mixin(opApplyImpl); }
public int opApply(scope int delegate(string, scope ref       
TOMLValue) @safe   pure dg) @safe pure         { 
mixin(opApplyImpl); }
public int opApply(scope int delegate(string, scope ref const 
TOMLValue) @safe   pure dg) @safe pure         { 
mixin(opApplyImpl); }
public int opApply(scope int delegate(string,       ref const 
TOMLValue) @safe   pure dg) @safe pure const   { 
mixin(opApplyImpl); }
public int opApply(scope int delegate(string, scope ref const 
TOMLValue) @safe   pure dg) @safe pure const   { 
mixin(opApplyImpl); }
public int opApply(scope int delegate(string,       ref       
TOMLValue) @system dg)      @system            { 
mixin(opApplyImpl); }
public int opApply(scope int delegate(string,       ref const 
TOMLValue) @system dg)      @system            { 
mixin(opApplyImpl); }
public int opApply(scope int delegate(string, scope ref       
TOMLValue) @system dg)      @system            { 
mixin(opApplyImpl); }
public int opApply(scope int delegate(string, scope ref const 
TOMLValue) @system dg)      @system            { 
mixin(opApplyImpl); }
public int opApply(scope int delegate(string,       ref const 
TOMLValue) @system dg)      @system const      { 
mixin(opApplyImpl); }
public int opApply(scope int delegate(string, scope ref const 
TOMLValue) @system dg)      @system const      { 
mixin(opApplyImpl); }
public int opApply(scope int delegate(string,       ref       
TOMLValue) @system pure dg) @system pure       { 
mixin(opApplyImpl); }
public int opApply(scope int delegate(string,       ref const 
TOMLValue) @system pure dg) @system pure       { 
mixin(opApplyImpl); }
public int opApply(scope int delegate(string, scope ref       
TOMLValue) @system pure dg) @system pure       { 
mixin(opApplyImpl); }
public int opApply(scope int delegate(string, scope ref const 
TOMLValue) @system pure dg) @system pure       { 
mixin(opApplyImpl); }
public int opApply(scope int delegate(string,       ref const 
TOMLValue) @system pure dg) @system pure const { 
mixin(opApplyImpl); }
public int opApply(scope int delegate(string, scope ref const 
TOMLValue) @system pure dg) @system pure const { 
mixin(opApplyImpl); }
```

Surely there is a better way to do this?!

Better formatted:

![formatted code](https://wfr.moe/f6PQlp.png)

(note: I don't want to use a template, this way of writing it has 
the advantage that the compiler checks all different code paths 
for errors, so the errors aren't delayed until someone actually 
tries to iterate over my data structure)


More information about the Digitalmars-d-learn mailing list