From 6d4e71c111226591a4eeb5b77efac689ef1dd79a Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Sun, 18 Aug 2013 15:36:18 -0700 Subject: Refinement of name-based unapplySeq. Can't finnesse the drop method. Call it blindly for now, even though in the long run you won't have to write drop. --- test/files/run/string-extractor.check | 5 +++++ test/files/run/string-extractor.scala | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) (limited to 'test/files/run') diff --git a/test/files/run/string-extractor.check b/test/files/run/string-extractor.check index 7268e44da9..47f3722c86 100644 --- a/test/files/run/string-extractor.check +++ b/test/files/run/string-extractor.check @@ -2,3 +2,8 @@ by BY oTheClown nope +1: ob +2: obby +2: OBBY +3: BOBO +3: TomTomTheClown diff --git a/test/files/run/string-extractor.scala b/test/files/run/string-extractor.scala index 4fb977df0b..c0fe911ff3 100644 --- a/test/files/run/string-extractor.scala +++ b/test/files/run/string-extractor.scala @@ -11,9 +11,26 @@ final class StringExtract(val s: String) extends AnyVal { override def toString = s } +final class ThreeStringExtract(val s: String) extends AnyVal { + def isEmpty = (s eq null) || (s == "") + def get: (List[Int], Double, ThreeStringExtract) = ((s.length :: Nil, s.length.toDouble, this)) + def length = s.length + def lengthCompare(n: Int) = s.length compare n + def apply(idx: Int): Char = s charAt idx + def head: Char = s charAt 0 + def tail: String = s drop 1 + def drop(n: Int): ThreeStringExtract = new ThreeStringExtract(s drop n) + + override def toString = s +} + + object Bippy { def unapplySeq(x: Any): StringExtract = new StringExtract("" + x) } +object TripleBippy { + def unapplySeq(x: Any): ThreeStringExtract = new ThreeStringExtract("" + x) +} object Test { def f(x: Any) = x match { @@ -21,10 +38,23 @@ object Test { case _ => "nope" } + def g(x: Any): String = x match { + case TripleBippy(3 :: Nil, 3.0, 'b', chars @ _*) => "1: " + chars + case TripleBippy(5 :: Nil, 5.0, 'b' | 'B', chars @ _*) => "2: " + chars + case TripleBippy(_, _, chars @ _*) => "3: " + chars + case _ => "nope" + } + def main(args: Array[String]): Unit = { println(f("Bobby")) println(f("BOBBY")) println(f("BoBoTheClown")) println(f("TomTomTheClown")) + + println(g("bob")) + println(g("bobby")) + println(g("BOBBY")) + println(g("BOBO")) + println(g("TomTomTheClown")) } } -- cgit v1.2.3