📄 patmatnew.scala
字号:
case (_, Nil) => 'b' case (h1 :: t1, h2 :: t2) => 'c' } def k (x:AnyRef) = x match { case null => 1 case _ => 2 } val FooBar = 42 def lala() = 42 match { case FooBar => true } object Bug1270 { // unapply13 class Sync { def apply(x: Int): Int = 42 def unapply(scrut: Any): Option[Int] = None } class Buffer { object Get extends Sync var ps: PartialFunction[Any, Any] = { case Get(y) if y > 4 => // y gets a wildcard type for some reason?! hack } } println((new Buffer).ps.isDefinedAt(42)) } object Bug1261 { sealed trait Elem case class Foo extends Elem case class Bar extends Elem trait Row extends Elem object Row { def unapply(r: Row) = true def f(elem: Elem) { elem match { case Bar() => ; case Row() => ; case Foo() => ; // used to give ERROR (unreachable code) }}} }/* object Feature1196 { def f(l: List[Int]) { } val l: Seq[Int] = List(1, 2, 3) l match { case x @ List(1, _) => f(x) // x needs to get better type List[int] here } }*/ object TestIfOpt { //compile-only "test EqualsPatternClass in combination with MixTypes opt, bug #1278" trait Token { val offset : Int def matching : Option[Token] } def go(tok : Token) = (tok.matching: @unchecked) match { case Some(other) if true => Some(other) case _ if true => tok.matching match { case Some(other) => Some(other) case _ => None } } } object Go { // bug #1277 compile-only trait Core { def next : Position = null } trait Dir val NEXT = new Dir{} trait Position extends Core (null:Core,null:Dir) match { case (_, NEXT) if true => false // no matter whether NEXT test succeed, cannot throw column because of guard case (at2:Position,dir) => true } } trait Outer { // bug #1282 compile-only object No trait File { (null:AnyRef) match { case No => false } } } object cast2 { // #1281 class Sync { def unapplySeq(scrut: Int): Option[Seq[Int]] = { println("unapplySeq: "+scrut) if (scrut == 42) Some(List(1, 2)) else None } } class Buffer { val Get = new Sync val jp: PartialFunction[Any, Any] = { case Get(xs) => println(xs) // the argDummy <unapply-selector> should have proper arg.tpe (Int in this case) } } println((new Buffer).jp.isDefinedAt(40)) println((new Buffer).jp.isDefinedAt(42)) } object ClassDefInGuard extends TestCase("classdef in guard") { // compile-and-load only val z:PartialFunction[Any,Any] = { case x::xs if xs.forall { y => y.hashCode() > 0 } => 1 } override def runTest { val s:PartialFunction[Any,Any] = { case List(4::xs) => 1 case List(5::xs) => 1 case _ if false => case List(3::xs) if List(3:Any).forall { g => g.hashCode() > 0 } => 1 } z.isDefinedAt(42) s.isDefinedAt(42) // just load the thing, to see if the classes are found (None:Option[Boolean] @unchecked) match { case x if x.map(x => x).isEmpty => } } } // bug#457 object Bug457 extends TestCase("Bug457") { def method1() = { val x = "Hello, world"; val y = 100; y match { case _: Int if (x match { case t => t.trim().length() > 0 }) => false; case _ => true; }} def method2(): scala.Boolean = { val x: String = "Hello, world"; val y: scala.Int = 100; { var temp1: scala.Int = y; var result: scala.Boolean = false; if ( { var result1: scala.Boolean = true; if (y == 100) result1 else throw new MatchError("crazybox.scala, line 11") } && (y > 90) ) result else throw new MatchError("crazybox.scala, line 9") }} override def runTest { method1(); method2(); } } // bug#508 object Bug508 extends TestCase("aladdin #508") { case class Operator(x: Int); val EQ = new Operator(2); def analyze(x: Pair[Operator, Int]) = x match { case Pair(EQ, 0) => "0" case Pair(EQ, 1) => "1" case Pair(EQ, 2) => "2" } override def runTest { val x = Pair(EQ, 0); assertEquals("0", analyze(x)); // should print "0" val y = Pair(EQ, 1); assertEquals("1", analyze(y)); // should print "1" val z = Pair(EQ, 2); assertEquals("2", analyze(z)); // should print "2" } } // bug#789 object Bug789 extends TestCase("aladdin #789") { // don't do this at home trait Impl trait SizeImpl extends Impl { def size = 42 } trait ColorImpl extends Impl { def color = "red" } type Both = SizeImpl with ColorImpl def info(x:Impl) = x match { case x:Both => "size "+x.size+" color "+x.color // you wish case x:SizeImpl => "!size "+x.size case x:ColorImpl => "color "+x.color case _ => "n.a." } def info2(x:Impl) = x match { case x:SizeImpl with ColorImpl => "size "+x.size+" color "+x.color // you wish case x:SizeImpl => "!size "+x.size case x:ColorImpl => "color "+x.color case _ => "n.a." } override def runTest { // make up some class that has a size class MyNode extends SizeImpl assertEquals("!size 42", info(new MyNode)) assertEquals("!size 42", info2(new MyNode)) } } // bug#995 object Bug995 extends TestCase("aladdin #995") { def foo(v: Any): String = v match { case s: Seq[_] => "Seq" // see hack in object Seq.unapplySeq //case a: AnyRef if runtime.ScalaRunTime.isArray(a) => "Array" case _ => v.toString } override def runTest { assertEquals("Seq", foo(Array(0))) } } // bug#1093 (contribution #460) object Bug1093 extends TestCase("aladdin #1093") { override def runTest {assertTrue(Some(3) match { case Some(1 | 2) => false case Some(3) => true })} } // bug#1094 (contribution #461) object Bug1094 extends TestCase("aladdin #1094") { def foo(ps: String*) = "Foo" case class X(p: String, ps: String*) def bar = X("a", "b") match { case X(p, ps @ _*) => foo(ps : _*) } override def runTest { assertEquals("Foo", bar) } } // #2 class Outer_2 { case class Foo(x: int, y: int) { override def equals(other: Any) = other match { case Outer_2.this.Foo(`x`, `y`) => true case _ => false } } } object Ticket2 extends TestCase("#2") { override def runTest { val o1 = new Outer_2; val o2 = new Outer_2; val x: Any = o1.Foo(1, 2); val y: Any = o2.Foo(1, 2) assertFalse("equals test returns true (but should not)", x equals y) assertTrue("match enters wrong case", x match { case o2.Foo(x, y) => false; case o1.Foo(x, y) => true case _ => false }) }}// #11 class MyException1 extends Exception // Commenting out the following line and uncommenting the second line // will cause the test to succeed. trait SpecialException extends MyException1 // trait SpecialException class MyException2 extends MyException1 with SpecialException object Ticket11 extends TestCase("#11") { override def runTest { Array[Throwable](new Exception("abc"), new MyException1, new MyException2).foreach { e => try { throw e } catch { case e : SpecialException => { assume(e.isInstanceOf[SpecialException]) } case e => { assume(e.isInstanceOf[Throwable]) } } } } } // #37 object Ticket37 extends TestCase("#37") { def foo() {} val (a,b) = { foo(); (2,3) } override def runTest { assertEquals(this.a, 2) } } // #44 trait _X { case class _Foo(); object _Bar { def unapply(foo: _Foo):Boolean = true; } } object Y extends _X { val foo = _Foo() foo match { case _Bar() => case _ => assert(false) } } object Ticket44 extends TestCase("#44") { override def runTest { assert(Y.toString ne null) /*instantiate Y*/ } } object Ticket211 extends TestCase("#211") { override def runTest { (Some(123):Option[Int]) match { case (x:Option[a]) if false => {}; case (y:Option[b]) => {}; } } } sealed abstract class Tree case class Node(l: Tree, v: Int, r: Tree) extends Tree case object EmptyTree extends Tree object Ticket335 extends TestCase("#335") { // compile-only override def runTest { (EmptyTree: Tree @unchecked) match { case Node(_,v,_) if (v == 0) => 0 case EmptyTree => 2 } } }// this test case checks nothing more than whether// case N for object N is translated to a check scrutinee.equals(N)// (or the other way round)... for a long time, we got away with// scrutinee eq N, but those golden days are, apparently, over. object Ticket346 extends TestCase("#346") {class L(val content: List[Int]) { def isEmpty = content.isEmpty def head = content.head def tail = content.tail override def equals(that: Any): Boolean = { val result = that.isInstanceOf[N.type] println("L("+content+").equals("+that+") returning "+result) result }}object N extends L(Nil) { override def equals(that: Any): Boolean = { val result = (that.isInstanceOf[L] && that.asInstanceOf[L].isEmpty) //println("N.equals("+that+") returning "+result) result }}object C { def unapply(xs: L): Option[(Int, L)] = { if (xs.isEmpty) { println("xs is empty"); None } else Some((xs.head, new L(xs.tail))) }} def empty(xs : L) : Boolean = xs match { case N => true case _ => false } def singleton(xs : L) : Boolean = xs match { case C(_, N) => true case _ => false }override def runTest() { assertTrue(empty( new L(Nil) )) assertTrue(singleton( new L(List(1)) ))}} // end Ticket346 object Ticket495bis { // compile-only def signum(x: Int): Int = x match { case 0 => 0 case _ if x < 0 => -1 case _ if x > 0 => 1 } def pair_m(x: Int, y: Int) = (x,y) match { case (_, 0) => 0 case (-1, _) => -1 case (_, _) => 1 } } object Ticket522 { // compile-only class Term[X] object App { // i'm hidden case class InternalApply[Y,Z](fun:Y=>Z, arg:Y) extends Term[Z] def apply[Y,Z](fun:Y=>Z, arg:Y): Term[Z] = new InternalApply[Y,Z](fun,arg) def unapply[X](arg: Term[X]): Option[(Y=>Z,Y)] forSome {type Y; type Z} = arg match { case i:InternalApply[y,z] => Some(i.fun, i.arg) case _ => None } } App({x: Int => x}, 5) match { case App(arg, a) => } } // end Ticket522 object Ticket710 { // compile-only def method: Unit = { sealed case class Parent case object Child extends Parent val x: Parent = Child x match { case Child => () } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -