Something like ADL from C++?
Timon Gehr
timon.gehr at gmx.ch
Wed Dec 4 02:51:25 UTC 2024
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.
More information about the Digitalmars-d
mailing list