You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Welcome to Scala2.13.5 (OpenJDK64-BitServerVM, Java11.0.9.1).
Type in expressions for evaluation. Ortry:help.
scala>importscala.util.parsing.combinator.RegexParsers||caseclassWordFreq(word: String, count: Int) {
|overridedeftoString=s"Word <$word> occurs with frequency $count"| }
||classSimpleParserextendsRegexParsers {
|defword:Parser[String] ="""[a-z]+""".r ^^ { _.toString }
|defnumber:Parser[Int] ="""(0|[1-9]\d*)""".r ^^ { _.toInt }
|deffreq:Parser[WordFreq] = word ~ number ^^ { case wd ~ fr =>WordFreq(wd,fr) }
| }
||objectTestSimpleParserextendsSimpleParser {
|defmain(args: Array[String]) = {
| parse(freq, "johnny 121") match {
|caseSuccess(matched,_) => println(matched)
|caseNoSuccess(msg,_) => println(s"NoSuccess: $msg")
| }
| }
| }
parse(freq, "johnny 121") match {
^On line 15:warning: match may not be exhaustive.
It would fail on the following inputs: Error(_, _), Failure(_, _)
importscala.util.parsing.combinator.RegexParsersclassWordFreqclassSimpleParserobjectTestSimpleParser
problem
To simplicity, it seems like something following (not exactly)
sealedabstractclassParseResultcaseclassSuccess(result: String) extendsParseResultsealedabstractclassNoSuccess(msg: String) extendsParseResultcaseclassFailure(msg: String) extendsNoSuccess(msg)
caseclassError(msg: String) extendsNoSuccess(msg)
objectNoSuccess {
defunapply[T](x: ParseResult):Option[String] = x match {
caseFailure(msg) =>Some(msg)
caseError(msg) =>Some(msg)
case _ =>None
}
}
The NoSuccess is a sealed abstact class and only has two sub-class Failure and Error.
The ParseResult is a sealed abstact class and only has two sub-class Success and NoSuccess
The matching here for Success and NotSuccess should be exhaustived already.
The text was updated successfully, but these errors were encountered:
counter2015
changed the title
Incorrect match inexhaustive warnings in Scala 2.13.5 in parser result.
Incorrect match inexhaustive warnings in Scala 2.13.5.
Apr 26, 2021
There's nothing unexpected here. With NoSuccess not being a case class case NoSuccess(msg,_) is using NoSuccess's unapply, which is a refutable custom extractor (you can tell because it returns an Option[String]). It's only by your analysis of the body of the unapply do you know that it extracts for both Error and Failure.
There's a few ways this could be fixed, at least in theory (backwards compatibility of scala-parser-combinators might impede parts of these):
define NoSuccess's unapply as def unapply[T](x: NoSuccess): Some[String], which I think should be fine
make NoSuccess the case class, and Error and Failure subclasses, and recreate their apply/unapply/toString/copy/etc methods
reproduction steps
using Scala 2.13.5, scala-parser-combinators 1.1.2,
the exmaple code is modified from scala-parser-combinators project.
problem
To simplicity, it seems like something following (not exactly)
The
NoSuccess
is a sealed abstact class and only has two sub-classFailure
andError
.The
ParseResult
is a sealed abstact class and only has two sub-classSuccess
andNoSuccess
The matching here for
Success
andNotSuccess
should be exhaustived already.The text was updated successfully, but these errors were encountered: