Something like ADL from C++?

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Fri Dec 6 08:05:02 UTC 2024


On 06/12/2024 8:59 PM, Arafel wrote:
> On 6/12/24 2:18, Richard (Rikki) Andrew Cattermole wrote:
>> We do not have runtime reflection. We cannot copy languages that do, 
>> including for your example that isn't an alternative to runtime 
>> reflection.
>>
>> ```d
>> interface ISerialize {
>>      void serialize();
>> }
>>
>> class SerializableRoot {
>>      void serialize() {
>>          // serialize here
>>      }
>> }
>>
>> class MyType : SerializableRoot {
>>      @thing int field;
>>
>>      ...
>> }
>> ```
>>
>> Some how SerializableRoot.serialize has to be able to see the child 
>> MyType class. We have no good way of handling that currently.
> 
> ```d
> import std;
> 
> interface ISerialize {
>      void serialize();
> }
> 
> struct thing{ }
> 
> class SerializableRoot {
>      void serialize(this T)() {
>          // serialize here, possibly using __traits to introspect on T
>          writeln("Serializing ", typeid(T));
>      }
> }
> 
> class MyType : SerializableRoot {
>      @thing int field;
> }
> 
> void main() {
>      MyType c = new MyType;
>      c.serialize;
> }
> ```

I'm fully aware of template this parameters, I was trying to get them to 
work 10 years ago.

They have problems that prevent their usage.

1. They are templates, so not in vtable
2. They require the this pointer to be typed to the child (extremely 
easy for this to not be the case, see 1)
3. Because they are not virtual, you cannot up cast (see 2)
4. Cannot be over ridden for any manual behavior

All and all, they become very complex for common usage, quite fragile if 
you don't use them right, not the kind of thing you recommend to people 
who are use to runtime reflection.



More information about the Digitalmars-d mailing list