PSA: Failed opDispatch`s dont memoize
monkyyy
crazymonkyyy at gmail.com
Tue Sep 9 20:38:25 UTC 2025
```d
import std;
enum counter=cast(immutable(void)*)[0].ptr;//ct counter, ignore
auto getcount()=>(*(cast(int*)counter));
auto count()=>(*(cast(int*)counter))++;
struct foo{
void opDispatch(string s)(){
enum _=count();//increases getcount by 1 each time its
compiled
static assert(0);
}
}
void bar(foo){}
void foobar(foo){}
unittest{
foo().bar;
foo().bar;
}
unittest{
foo().bar;
foo().foobar;
getcount.writeln;//4
}
```
`foo().bar` is being parsed and compiled 3 times; my instincts
say this could be very slow for "builder patterns" and you may
want to keep that in mind for large code bases that are slow if
opDispatch (maybe with complex contracts?) is the only way to
write something
More information about the Digitalmars-d
mailing list