How to specity a list of fields with default to a mixin template?

Inkrementator invalid at invalid.org
Sun Mar 2 23:28:09 UTC 2025


On Sunday, 2 March 2025 at 19:31:06 UTC, realhet wrote:
> Anyone have an idea?

While template mixins have access to the caller scope, the 
default values for parameters apparently don't. If you `inline` 
the default value, it will work

```
import std.traits;
mixin template T(string def)
{
	enum generatedStr = FieldTypeTuple!(typeof(mixin("new 
class{"~def~"}"))).stringof;
}

void main()
{
	mixin T!"int a, b;" mixin1;
	pragma(msg, mixin1.generatedStr);
	struct LocalStruct{ int aaa; }
	mixin T!"LocalStruct ls;" mixin2;
	pragma(msg, mixin2.generatedStr);
}
```


More information about the Digitalmars-d-learn mailing list