forked from boostorg/spirit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] is_substitute for variant types
Fix boostorg#721, boostorg#722. Experimental: use optional<variant>
- Loading branch information
Showing
5 changed files
with
49 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7e62f90
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.
This variant<A,B> and variant<B,A> being treated as "equivalent" would require changing:
spirit/doc/x3/quick_reference.qbk
Line 299 in 73a865b
somehow to reflect that. How would that be expressed? Maybe:
[``a: A, b: B --> (a | b): variant<permutation<A, B>>
where permutation<A,B> is defined as either variant<A,B> or variant<B,A>?
7e62f90
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.
Hi @cppljevans,
Thanks for the review.
I'm not sure about this. I'd expect that any "sane" variant type would allow permutation of types, or converting between two variant where the "lhs" one has a superset of the types of the rhs one. In other words, I would expect the following:
boost::variant supports this. I didn't test yet boost::variant2 or std::variant.
In addition to that, now that I think about it, tests (and maybe infrastructure) are needed to assert the compatibility between variants over compatible fusion sequences. That is,
7e62f90
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.
This question of conversion between variant types I've just explored and found they aren't. For example, as mentioned elsewhere, this issue707 test code fails with this variant_substitute code; However, when issue707 test code is changed to contain:
then it only fails when defined(USE_X3_VARIANT). So, should x3::variant be outlawed to allow issue707 to be resolved?
7e62f90
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.
You're right: x3::variant doesn't support that direct assignment from another x3::variant with permuted types.
Outlawing variant is out of question, but, as I've already mentioned in my comments in the issue in the main spirit repo, the fix in this branch is crude.
I can see no reasons for which
variant<A, B>
should not be "convertible" tovariant<B, A>
: if a sum type can hold A and B, their order in the declaration of the type shouldn't matter.This also applies to different "species" of variant. One should be able to conjure code to assign
boost::variant<A, B>
tox3::variant<A, B>
orstd::variant<A, B>
, regardless of the order of the types.What it's needed IMHO is a proper definition of is_substitute for variants, and corresponding support in x3::move_to (and maybe elsewhere).
Supposing that lhs and rhs are recognized by x3 as variant types, I'm thinking along the lines of
I'll try to give this a shot when I have some time to spare.
7e62f90
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.
Interesting fact: std::variant doesn't support assignment from permuted types as well. boost::variant2 however does.
7e62f90
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.
Hi @cppljevans,
JFYI, I stumbled on some surprising behavior of boost::variant, which apparently doesn't play well with is_constuctible/is_assignable traits in presence of an ambiguous conversion or explicit constructor.
My plan to use those traits to improve conversion of variants in spirit can't work unless mentioned behavior gets fixed.
I've raised an issue and sent a pull request to boost::variant... let's see what comes out of it.
7e62f90
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.
Hi @eido79,
Does this change to boost::variant allow the issue707 test to pass?
7e62f90
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.
Apparently it doesn't. I tried using my patch to variant in combination with "vanilla" spirit and my patched version, but the issue remains.
From a cursory look, I doubt this is due to the issue I'm trying to address in variant, but I see in the comments to boostorg#707 that you have posted a possible solution.
Given that x3 is moving to C++17, I think it might be a good idea to try to move it forward.
I'll try to investigate further when I have some time to spare.
7e62f90
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.
@eido79, the possible solution I mentioned won't work because, sadly, it doesn't account for permuting of the variant args. I just hadn't considered that.
During my search for that solution, I found adding debug _enabler classes to is_substitute.hpp code very helpful.
Please see is_substitute_impl_enabler. That may help you in seeing where things might go wrong. HOWEVER, it does require other code to be cloned; hence, that may be too much trouble for you. I don't know, but thought I'd offer.
7e62f90
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.
Hi,
As a quick test, I replaced x3::variant with boost::variant, which supports permutations, but the result didn't change.
is_substitute_impl_enabler looks quite useful, thanks! I'll definitely keep it in mind in case I need something like that.
7e62f90
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.
Hi,
It's been a while, but I've finally found some time to come back to this.
All in all: it's a mess. The more I look into this, the more I believe that the way spirit handles variants, especially when parsing into containers, needs to be deeply revisited.
I hope to have some quality time to dedicate to this, as this is going to require some effort even to start.
7e62f90
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.
@eido79 , I just uploaded an update to my unfold_left.
The updates should enable compilation with any tests you have. The update simplifies code, as explained in the README.md file.
HTH.
-Larry
7e62f90
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.
Hi @cppljevans,
Thanks for the info. Between family and ${dayjob} I'm unfortunately having virtually no spare time to dedicate to side projects, but I'll definitely check your branch out when I can.
7e62f90
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.
Unfortunately, the unfold_left code fails with the expect.cpp test:
https://github.com/boostorg/spirit/blob/32fb6f689957c121973594e2505fd959a917291e/test/x3/expect.cpp#L108
It has something to do with unfold_left detail/sequence.hpp having no replacement for partition_attribute:
https://github.com/boostorg/spirit/blob/32fb6f689957c121973594e2505fd959a917291e/include/boost/spirit/home/x3/operator/detail/sequence.hpp#L124
Trying to figure a partition_attribute replacement.