Wrapper around a recursive data type
drug007
drug2004 at bk.ru
Mon Jul 8 08:56:51 UTC 2024
I need to generate some meta info of my data types. I do it this
[way](https://gist.github.com/drug007/b0a00dada6c6ecbf46b4f6988401d4e2):
```D
import std.range, std.format, std.stdio, std.traits;
struct NestedType
{
string s;
double d;
}
struct Node
{
string value;
int i;
NestedType nt;
// Node[] children; // It makes Node to be a recursive data types
}
template Meta(A)
{
static if (isRandomAccessRange!A)
alias Meta = RaRMeta!A;
else static if (isSomeString!A || isIntegral!A || isFloatingPoint!A)
alias Meta = ScalarMeta!A;
else
alias Meta = AggregateMeta!A;
}
struct RaRMeta(A)
{
alias ElementType = typeof(A.init[0]);
Meta!ElementType[] model;
}
struct ScalarMeta(A) {}
struct AggregateMeta(A)
{
static foreach (member; __traits(allMembers, A))
mixin("Meta!(typeof(A.%1$s)) %1$s;".format(member));
}
void main()
{
auto meta = Meta!Node();
assert(meta.value == ScalarMeta!string());
assert(meta.i == ScalarMeta!int());
assert(meta.nt == AggregateMeta!NestedType());
assert(meta.nt.s == ScalarMeta!string());
assert(meta.nt.d == ScalarMeta!double());
}
```
It works. But when I try to wrap around a recursive data type (uncomment
`children` member of `Node` type) it fails because during instantiating
of `Meta!Node` it needs complete `Meta!Node` type to process `children`
member of `Node`.
How can I "break" this recursion or some other work around to fix it?
More information about the Digitalmars-d-learn
mailing list