Would be really cool to get some monadic operators like andThen()
, and orElse()
on juce::ValueTree
. This seems to be the direction many of the STL containers are taking and I for one find they can help write some really clean code.
juce::ValueTree::andThen()
should take a function to act on a juce::ValueTree
when it is non-empty, and likewise juce::ValueTree::orElse()
should take a function to act on a juce::ValueTree
when it is empty - allowing for some nice error handling.
This would be particularly useful for things like loading from XML:
const auto validVtXML = R"(<Tree foo="123"/>)";
const auto invalidVtXML = R"(<Tree>Some inline text</Tree>)";
juce::ValueTree::fromXML (validVtXML)
.orElse ([](auto& /*emptyTree*/) {
return juce::ValueTree {"Tree"}; // Some fallback state
})
.andThen ([] (auto& populatedTree) {
loadState (populatedTree);
return populatedTree;
});
The current way of doing this would be something like:
auto state = juce::ValueTree::fromXML (invalidVtXML);
if (state == juce::ValueTree{})
state = juce::ValueTree {"Tree"};
loadState (state);
Which is fine if you prefer more imperitive code, but I personally much prefer the more declarative style.