-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add parsing of pointers #63
base: v2.x.x
Are you sure you want to change the base?
Add parsing of pointers #63
Conversation
Pointers can be useful if we want to allow using types recursively directly (i.e. not as an array or mapping), for example to express tree structures (decision trees, composite types, etc.).
b016834
to
7ac1825
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to stay away from using types that have an identity (that's why classes are not supported), but the recursive need makes sense. Just a note on initialization.
source/configy/read.d
Outdated
else static if (is(FR.Type == T*, T)) | ||
{ | ||
// Allocate and parse pointers' values. | ||
auto value = new T; | ||
*value = node.parseField!(NestedFieldRef!(T, FR))( | ||
path, T.init, ctx); | ||
return value; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That doesn't quite work for const
/ immutable
types though. A lot of the code in this module is convoluted to allow for const
& immutable
values to be used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. Fix pushed, not sure if that fix is the best way?
source/configy/test.d
Outdated
{ | ||
static struct N { | ||
@Optional int value; | ||
@Optional N* left, right; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think pointers should always be optional. I can't see a situation where they wouldn't be.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can think of one - when using mutually recursive types, only one point of recursion might be optional, e.g.:
import configy.attributes;
/// Node for an AST for some language that uses infix operators.
struct Node {
// Only one of these must be set.
@Optional BinaryExpression* addition;
@Optional BinaryExpression* subtraction;
// ...
}
struct BinaryExpression {
// You've chosen which kind of expression this is.
// Now, you MUST specify the operands, so these are not optional:
Node* left, right;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess left
and right
could just be Node
. Seems a bit weird though, with how ASTs are usually represented in memory. Might be asymmetric with other parts where Node
values are optional.
I'm curious to know why? I agree that pointers are not ideal, but my main issues with using them in this context would be that they come bundled with some semantics (pointer arithmetic, indexing, segmentation faults on null dereferences) that are not helpful here. |
Pointers can be useful if we want to allow using types recursively directly (i.e. not as an array or mapping), for example to express tree structures (decision trees, composite types, etc.).