Self-referential struct mixins
rbscott
tjonsies818 at yahoo.com
Sun Nov 17 07:13:52 UTC 2019
Hello,
I recently started using D, and I am exploring some of the
functionality in CTFE. I ran into a case that I can't get past.
The goal is to take a JSON API Schema and convert it into D types
using CTFE. The problem is, the types can be self-referential.
A simplified form of the language would look like:
{ "A" : { "type": struct, "fields" : { "b": { "type": "string" },
"a" : { "type": "A" }}}
This would yield a type "A" that has two fields, "a" and "b".
Where "a" is a string and "b" is of type "A". Or in D:
struct A {
string b;
A* a;
}
This is a simplified example, but imagine this is highly nested
to generate arbitrarily complex types that could reference on one
another.
I got this working in the simple case if none of the fields are
self-referential, but it breaks as soon as there is a loop.
Here is a gist which demonstrates the issue:
https://gist.github.com/rbscott/ee0f3ba94296f9c8224a8c4c13c2f026.
I think I can workaround this by with string mixins everywhere,
but the structure generation logic is complex enough that I would
prefer not to do that. This approach could be totally off, so any
suggestions would be greatly appreciated!
thanks,
rbscott
More information about the Digitalmars-d
mailing list