Contextual metaprogramming, query the parent type.

Nick Treleaven nick at geany.org
Fri Feb 4 15:56:05 UTC 2022


On Friday, 4 February 2022 at 09:41:57 UTC, Ola Fosheim Grøstad 
wrote:
> So, what if you could write a template that queries the 
> containing type and then either produces  
> ```IntFast!(-2,128)``` or  ```IntCompact!(-2,128)``` types?

I think you can do this but users would need to use a string 
mixin when defining the field. A function can obtain the parent 
symbol name using this trick:

```d
string fieldString(string parentName = __traits(parent, 
{}).stringof){
    ...
} 
struct Parent
{
     mixin(fieldString);
}
```


More information about the Digitalmars-d mailing list