digitalmars.D - PSA: Failed opDispatch`s dont memoize
- monkyyy (29/29) Sep 09 ```d
```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
Sep 09








monkyyy <crazymonkyyy gmail.com>