Skip to content
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

Open
wants to merge 2 commits into
base: v2.x.x
Choose a base branch
from

Conversation

VPanteleev-S7
Copy link
Contributor

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.).

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.).
@VPanteleev-S7 VPanteleev-S7 changed the title configy: Add parsing of pointers Add parsing of pointers Jan 15, 2025
Copy link
Member

@Geod24 Geod24 left a 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.

Comment on lines 825 to 832
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;
}
Copy link
Member

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.

Copy link
Contributor Author

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?

{
static struct N {
@Optional int value;
@Optional N* left, right;
Copy link
Member

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.

Copy link
Contributor Author

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;
}

Copy link
Contributor Author

@VPanteleev-S7 VPanteleev-S7 Jan 19, 2025

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.

@VPanteleev-S7
Copy link
Contributor Author

I tried to stay away from using types that have an identity

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants