Search code examples
parsinghaskellparser-combinators

Avoiding usage of fail in parsers from Parsers library


From what I was told using fail is not recommended and it will later be removed.

What should be properly used instead of fail in the following Parsers/Trifecta example?

parserNaturalNoLeadZero :: Parser Integer
parserNaturalNoLeadZero = do
  digits <- some digit
  if length digits > 1 && head digits == '0'
    then fail "Leading Zeros"
    else return $ read digits

Solution

  • As the documentation tells you, a new MonadFail class is being introduced to fulfill that role.

    But, for stuff like parsers, the sensible choice is usually empty, which has been around for much longer.