How to specity a list of fields with default to a mixin template?
realhet
real_het at hotmail.com
Sun Mar 2 19:31:06 UTC 2025
Hello,
```d
mixin template T(string def, alias C = typeof(mixin("new
class{"~def~"}")))
{
enum generatedStr = FieldTypeTuple!C.stringof;
}
void main()
{
{
mixin T!"int a, b;";
pragma(msg, generatedStr); //works
}
{
struct LocalStruct(){ int aaa; }
mixin T!"LocalStruct ls;"; //can't see localStruct
pragma(msg, generatedStr);
}
}
```
Is there a way to capture local scope in templates?
I've tried to put the expansion of the field declaration into the
template parameter list, but it is still can't see my local types.
My problem is that I already have a robust Class
field/constructor/declarator/parent/child/list generator thing,
and I choosed to specify the list of field declaration by mixing
in a string into a D construct. I tried function parameters, and
now in the example, there is a mixed in anonym class.
But now in whatever modules I want to use my 'Smart" classes I
have to replicate this same header code to ensure it can see the
local declarations.
Now it was good for half a year, but today I found out that if a
module with a generator template uses another module with a
generator template, the imported module will have 2 generators
and wil fail.
I would be able to solve this by:
```d
mixin genFields!(AliasSeq!(int, string), ["field1", "field2"],
AliasSeq!(5, "str"/+default values+/));
```
But this is not so nice.
This is the nice form:
```d
mixin genFields!"int field1=5; string field2=`str`;";
```
But I unable to find a way to parse this declaration in the scope
where my types are.
I have to copy the genFields template into whatever module I want
to use it, and I must be careful not to import 2 or more
genFields to the same scope.
Anyone have an idea?
More information about the Digitalmars-d-learn
mailing list