On Wednesday, 4 October 2017 at 17:56:16 UTC, Walter Bright wrote:
Please present an example.

Let's say you're importing a C library which defines random function generators. They may be more random than Phobos rngs, they might be crytpo secure or whatever so you want to use them. It could be like:

module crng;

extern(c) struct Crng
{   int seedA;
    int seedB;
    ubyte[] restOfTheData;
}

extern(c) int current(Crng*);
extern(c) void toNextValue(Crng*);
extern(c) Crng initByTime(int unixTime);
//...

But you also want a probos-style range interface for them. You have to wrap the c struct into a new struct type:

struct Crng
{   crng.Crng _impl;
    alias _impl this;
    auto front(){return current(*_impl);}
    void popFront(){toNextValue(*_impl);}
    //...
}

and you have to rewrite many wrappers for Crng functions despite the alias this because they either require return Crng or gequire a pointer to one. This needs to be defined manually for example:

Crng initByTime(int time){return Crng(crng.initByTime(time))};

With ADL it would be enough to extend the original struct with range primitives:

auto front(Crng range){return current(*range);}
void popFront(ref Crng range){toNextValue(*range);}
//...

With current semantics the latter example will work only with locally defined range function templates. Not with Phobos ones, because they cannot see the extension functions.

Note, I do not have a D compiler in where I posted these from so they're not checked for compilation errors.

Reply via email to