swift-parsing icon indicating copy to clipboard operation
swift-parsing copied to clipboard

Bring the coalescing operator `??` to parsers

Open tgrapperon opened this issue 3 years ago • 0 comments

Hello! This PR adds two overloads for the nil-coalescing operator to sugar .replaceError(with:). It should improve reachability of ReplaceError parsers that are very frequently needed.

If the left/upstream parser fails, it falls back with the right/default value. If P<Output> is some upstream parser and default an Output value, the overall parser always returns default when the upstream P fails in some way:

P<Int> ?? 1   P fails, parses 1
P<Int?> ?? 1  P fails, parses 1
P<Int>? ?? 1  P fails or is nil, parses 1

I think this is what users would naturally expect.

It shouldn't impact existing parsers.

We could maybe expand/update the documentation around ReplaceError or Optionally. I think we can also document these overloads if they appear in docc. I'll update this PR with more documentation commits, should it go further.

tgrapperon avatar Jul 17 '22 14:07 tgrapperon