From b1553cb9c5894e2e91925a67afd2c986675e5c46 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Thu, 15 Dec 2016 15:21:26 +0100 Subject: Implement new rules for name-based pattern matching This implements the rules laid down in #1805. --- tests/pos/Patterns.scala | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'tests/pos/Patterns.scala') diff --git a/tests/pos/Patterns.scala b/tests/pos/Patterns.scala index aa369a77b..fd0d7e97a 100644 --- a/tests/pos/Patterns.scala +++ b/tests/pos/Patterns.scala @@ -108,3 +108,31 @@ object NestedPattern { val xss: List[List[String]] = ??? val List(List(x)) = xss } + +// Tricky case (exercised by Scala parser combinators) where we use +// both get/isEmpty and product-based pattern matching in different +// matches on the same types. +object ProductAndGet { + + trait Result[+T] + case class Success[+T](in: String, x: T) extends Result[T] { + def isEmpty = false + def get: T = x + } + case class Failure[+T](in: String, msg: String) extends Result[T] { + def isEmpty = false + def get: String = msg + } + + val r: Result[Int] = ??? + + r match { + case Success(in, x) => x + case Failure(in, msg) => -1 + } + + r match { + case Success(x) => x + case Failure(msg) => -1 + } +} -- cgit v1.2.3