Skip to content

Commit

Permalink
Teach patmat that unapplies can be customised
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed Aug 10, 2020
1 parent 98384fd commit e84e47d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Test.scala
@@ -0,0 +1,5 @@
object Test {
def main(args: Array[String]): Unit = {
Ttl(1) match { case Ttl(y) => println(y) }
}
}
7 changes: 7 additions & 0 deletions Ttl.scala
@@ -0,0 +1,7 @@
final case class Ttl(duration: Int, other: Boolean)

object Ttl {
def apply(duration: Int) = new Ttl(duration, false)

def unapply(x: Ttl): Option[Int] = if (x eq null) None else Some(x.duration)
}
Expand Up @@ -97,7 +97,7 @@ trait PatternTypers {
// Dueling test cases: pos/overloaded-unapply.scala, run/case-class-23.scala, pos/t5022.scala
// A case class with 23+ params has no unapply method.
// A case class constructor may be overloaded with unapply methods in the companion.
else if (canElide && caseClass.isCase && !member.isOverloaded)
else if (canElide && caseClass.isCase && !member.isOverloaded && (member == NoSymbol || member.isCaseApplyOrUnapply))
logResult(s"convertToCaseConstructor($fun, $caseClass, pt=$pt)")(convertToCaseConstructor(fun, caseClass, pt))
else if (!reallyExists(member))
CaseClassConstructorError(fun, s"${fun.symbol} is not a case class, nor does it have a valid unapply/unapplySeq member")
Expand Down
13 changes: 13 additions & 0 deletions test/files/pos/t11252.scala
@@ -0,0 +1,13 @@
final case class Ttl(duration: Int, other: Boolean)

object Ttl {
def apply(duration: Int) = new Ttl(duration, false)

def unapply(x: Ttl): Option[Int] = if (x eq null) None else Some(x.duration)
}

object Test {
def main(args: Array[String]): Unit = {
Ttl(1) match { case Ttl(y) => println(y) }
}
}

0 comments on commit e84e47d

Please sign in to comment.