Something like ADL from C++?
Manu
turkeyman at gmail.com
Thu Dec 5 02:36:02 UTC 2024
On Wed, 4 Dec 2024 at 12:56, Timon Gehr via Digitalmars-d <
digitalmars-d at puremagic.com> wrote:
> On 12/3/24 12:55, Manu wrote:
> > Maybe someone has a pattern for doing this kind of thing...
>
> There are a couple solutions that look okay already, but basically,
> importing into any unordered scope works, e.g.
>
> ```d
> module default_serialise;
> ubyte[] serialise(T)(T arg)if(!is(T==struct)){ return [1]; }
> ```
>
> ```d
> module user_code;
> struct S{}
> ubyte[] serialise(S){ return [2]; }
> ```
>
> ```d
> import default_serialise;
>
> void main(){
> import std;
> static struct Dummy{
> import default_serialise: serialise;
> import user_code: serialise;
> }
> import user_code;
> writeln(Dummy.serialise(1)); // [1]
> writeln(Dummy.serialise(S())); // [2]
> }
> ```
>
> You can also use a dummy template scope (template Dummy(), and then
> `Dummy!().serialise`).
>
> I dislike function-local import semantics quite a bit as they do not
> follow the well-thought-out overloading rules that apply to other imports.
>
"Unordered scope"; it never occurred to me that was a concept! But I can
see that now...
What a horrible hack, but I guess that could work.
I'll see if I can do anything with this idea...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20241205/5938f2a8/attachment-0001.htm>
More information about the Digitalmars-d
mailing list