toChoiceParser method

ChoiceParser<R> toChoiceParser({
  1. FailureJoiner? failureJoiner,
})

Combines this iterable of parsers into a single ChoiceParser.

Tries each parser in sequence, returning the result of the first successful match. If all parsers fail, a joined Failure is produced according to failureJoiner (defaults to selectLast).

Because all elements in the collection must share a single type parameter R, combining parsers of different types causes Dart to infer their closest common supertype (typically Object or Object?):

// Inferred as ChoiceParser<Object>:
final choice = [
  letter(),                  // Parser<String>
  digit().map(int.parse),     // Parser<int>
].toChoiceParser();

For homogeneous collections, the concrete type is preserved directly:

// Inferred as ChoiceParser<String>:
final keywords = [string('if'), string('else')].toChoiceParser();

Implementation

ChoiceParser<R> toChoiceParser({FailureJoiner? failureJoiner}) =>
    ChoiceParser<R>(this, failureJoiner: failureJoiner);