On Tuesday, 27 October 2015 at 14:00:07 UTC, Martin Nowak wrote:
On Tuesday, 27 October 2015 at 13:14:36 UTC, wobbles wrote:
How can `coordinates` member be known at compile-time when the input argument is a run-time string?

I suspect through the opDispatch operator overload.


Yikes, this is such an anti-pattern.

Heh - yeah it is quite problematic.

The only time I've needed to use it was when I was reading in Json with some structure like
    {  "timestamp" : { ... timestamp info ... },
       "info1" : { ... info ...},
       "info2" : { ... info ...},
       "info 23" : { ... info  ...}
    { < more of the above >}

and I wanted to be able get a Json[timestamp] map, where the Json is either a info1, info2 etc etc. I didn't want to write 23 different functions "hash_info1", "hash_info2" etc etc.
So, opDispatch!

Basically I wanted to hash the timestamp and some data. My opDispatch became:

        @ignore auto opDispatch(string name)(){
static assert(name.startsWith("hash_"), "Error, use StatHosts.hash_XYZ to gather XYZ[timestamp] info");
                static assert(name.length > 5);
                enum dataName = name[5..$];
                typeof(mixin("StatDetail."~dataName))[StatTimestampDetail] data;

                foreach(stat; statistics){
                        data[stat.timestamp] = mixin("stat."~dataName);
                return data;

23 functions merged into 1...

The static assert reduces the number of places it can break things at least, still some weird things can happen but for the most part it's ok.

So yes - opDispatch is cool but should be used VERY sparingly.

Reply via email to