In YAML parser, use a placeholder struct for empty sequences #387
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
In EAMxx, we were forced to add custom YAML tags (like
!!ints) to specify that a YAML sequence was meant to be a sequence of a particular type. The need comes from allowing strong type safety when dealing with empty lists, so that parsingmy_double_vec: []would indeed yield a param list storing astd::vector<double>of length 0. However, this added some clunkyness to our yaml file construction in EAMxx.This PR changes approach: when parsing a YAML file and encountering an empty sequence, we store a
EmptySeqobject. InParameterList, we then allow to callget<T>("my_seq")with anystd::vector<S>: if the param list is non-const, we will replace theEmptySeqentry with astd::vector<S>, while if the param list is const we return a const ref to a static local var.Testing
Added testing for this feature
Additional Information
There is a drawback in this approach: if the user accidentally does something like
and
my_doubleswas indeed still set to an instance ofEmptySeq, it will replace it with astd::vector<int>, which was not intended. However, I consider this a minor corner case. Getting rid of custom-made YAML tags is probably outweighing the "risk".