diff options
author | Eugene Burmako <xeno.by@gmail.com> | 2013-10-18 07:22:58 -0700 |
---|---|---|
committer | Eugene Burmako <xeno.by@gmail.com> | 2013-10-18 07:22:58 -0700 |
commit | 54707cb45018170e31eb188a9a694ab9b0728f71 (patch) | |
tree | fae843355f051d9aed9b223ecf18c28bee63ecd9 /test/files/run | |
parent | 5b2c4644a23ffbe1c45df40bf3511aee71eb0fe2 (diff) | |
parent | 210dbc7887bc42eed4154de65d0ff5f46ca5ee58 (diff) | |
download | scala-54707cb45018170e31eb188a9a694ab9b0728f71.tar.gz scala-54707cb45018170e31eb188a9a694ab9b0728f71.tar.bz2 scala-54707cb45018170e31eb188a9a694ab9b0728f71.zip |
Merge pull request #3030 from xeno-by/topic/fundep-views
SI-3346 implicit parameters can now guide implicit view inference
Diffstat (limited to 'test/files/run')
-rw-r--r-- | test/files/run/t3346a.check | 1 | ||||
-rw-r--r-- | test/files/run/t3346a.scala | 11 | ||||
-rw-r--r-- | test/files/run/t3346b.check | 0 | ||||
-rw-r--r-- | test/files/run/t3346c.check | 0 | ||||
-rw-r--r-- | test/files/run/t3346d.check | 0 | ||||
-rw-r--r-- | test/files/run/t3346d.scala | 21 | ||||
-rw-r--r-- | test/files/run/t3346e.check | 12 | ||||
-rw-r--r-- | test/files/run/t3346e.scala | 81 | ||||
-rw-r--r-- | test/files/run/t3346f.check | 2 | ||||
-rw-r--r-- | test/files/run/t3346f.scala | 15 | ||||
-rw-r--r-- | test/files/run/t3346g.check | 1 | ||||
-rw-r--r-- | test/files/run/t3346g.scala | 9 | ||||
-rw-r--r-- | test/files/run/t3346h.check | 1 | ||||
-rw-r--r-- | test/files/run/t3346h.scala | 9 | ||||
-rw-r--r-- | test/files/run/t3346j.check | 1 | ||||
-rw-r--r-- | test/files/run/t3346j.scala | 11 |
16 files changed, 175 insertions, 0 deletions
diff --git a/test/files/run/t3346a.check b/test/files/run/t3346a.check new file mode 100644 index 0000000000..d00491fd7e --- /dev/null +++ b/test/files/run/t3346a.check @@ -0,0 +1 @@ +1 diff --git a/test/files/run/t3346a.scala b/test/files/run/t3346a.scala new file mode 100644 index 0000000000..c0a90b011b --- /dev/null +++ b/test/files/run/t3346a.scala @@ -0,0 +1,11 @@ +import scala.language.implicitConversions + +object Test extends App { + class Rep[T](x : T) + + class SomeOps[T](x : Rep[T]) { def foo = 1 } + implicit def mkOps[X, T](x : X)(implicit conv: X => Rep[T]) : SomeOps[T] = new SomeOps(conv(x)) + + val a: Rep[Int] = new Rep(42) + println(a.foo) +}
\ No newline at end of file diff --git a/test/files/run/t3346b.check b/test/files/run/t3346b.check new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/test/files/run/t3346b.check diff --git a/test/files/run/t3346c.check b/test/files/run/t3346c.check new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/test/files/run/t3346c.check diff --git a/test/files/run/t3346d.check b/test/files/run/t3346d.check new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/test/files/run/t3346d.check diff --git a/test/files/run/t3346d.scala b/test/files/run/t3346d.scala new file mode 100644 index 0000000000..3f79896210 --- /dev/null +++ b/test/files/run/t3346d.scala @@ -0,0 +1,21 @@ +import scala.language.implicitConversions + +object Test extends App { + trait TARInt + + trait Basket[A,B] { + def iAmABasket = {} + } + + trait BasketFactory[A,B] { + def create(v: A): Basket[A,B] + } + + implicit val bf = new BasketFactory[Int,TARInt] { + def create(v: Int): Basket[Int,TARInt] = new Basket[Int, TARInt]{} + } + + implicit def i2[A,B](a: A)(implicit bf: BasketFactory[A,B]): Basket[A,B] = bf.create(a) + + 1.iAmABasket // <-- i2 conversion not applicable +}
\ No newline at end of file diff --git a/test/files/run/t3346e.check b/test/files/run/t3346e.check new file mode 100644 index 0000000000..71a57ffa70 --- /dev/null +++ b/test/files/run/t3346e.check @@ -0,0 +1,12 @@ +eqw +List(0, 2) +List(0, 2) +BitSet(0, 2) +Vector(113, 119, 101) +qwe +List(2, 0) +List(0!) +BitSet(0, 2) +qwe +List(2, 0) +qwe diff --git a/test/files/run/t3346e.scala b/test/files/run/t3346e.scala new file mode 100644 index 0000000000..ac0de564d4 --- /dev/null +++ b/test/files/run/t3346e.scala @@ -0,0 +1,81 @@ +import scala.language.implicitConversions +import scala.collection.generic.CanBuildFrom +import scala.math.Ordering +import collection.{TraversableLike, SeqLike} +import collection.immutable.BitSet + +class QuickSort[Coll](a: Coll) { + //should be able to sort only something with defined order (someting like a Seq) + def quickSort[T](implicit ev0: Coll => SeqLike[T, Coll], + cbf: CanBuildFrom[Coll, T, Coll], + n: Ordering[T]): Coll = { + quickSortAnything(ev0, cbf, n) + } + + //we can even sort a Set, if we really want to + def quickSortAnything[T](implicit ev0: Coll => TraversableLike[T, Coll], + cbf: CanBuildFrom[Coll, T, Coll], + n: Ordering[T]): Coll = { + import n._ + if (a.size < 2) { + a + } else { + // We pick the first value for the pivot. + val pivot = a.head + val (lower, tmp) = a.partition(_ < pivot) + val (upper, same) = tmp.partition(_ > pivot) + val b = cbf() + b.sizeHint(a.size) + b ++= new QuickSort(lower).quickSortAnything + b ++= same + b ++= new QuickSort(upper).quickSortAnything + b.result + } + } +} + +class FilterMap[Repr](a: Repr) { + def filterMap[A, B, That](f: A => Option[B])(implicit ev0: Repr => TraversableLike[A, Repr], + cbf: CanBuildFrom[Repr, B, That]): That = { + a.flatMap(e => f(e).toSeq) + } +} + +class FilterMapFixed[A, Repr <% TraversableLike[A, Repr]](a: Repr) { + def filterMap2[B, That](f: A => Option[B])(implicit cbf: CanBuildFrom[Repr, B, That]): That = { + a.flatMap(e => f(e).toSeq) + } +} + +object MyEnhancements { + implicit def toQS[Coll](a: Coll) = new QuickSort(a) + implicit def toFM[Coll](a: Coll) = new FilterMap(a) + implicit def toFM2[A, Repr <% TraversableLike[A, Repr]](a: Repr) = new FilterMapFixed(a) +} + +object Test extends App { + + import MyEnhancements._ + + println("qwe".quickSort) + println(Array(2, 0).quickSort.toList) + println(Seq(2, 0).quickSort) + //not very useful to sort a set, but just as a demonstration + println(BitSet(2, 0).quickSortAnything) + + //need to hint type inferencer, + //probably will be able to overcome after https://issues.scala-lang.org/browse/SI-4699 and + // related issues are fixed (by moving ev0 parameter from filterMap to toFM), see toFM2 + println("qwe".filterMap((c: Char) => Some(c.toInt))) + println("qwe".filterMap((c: Char) => Some(c))) + println(Array(2, 0).filterMap((c: Int) => Some(c.toInt)).toList) + println(Seq(2, 0).filterMap((c: Int) => if (c < 2) Some(c + "!") else None)) + def test(i:Int) = Option(i) + println(BitSet(2,0).filterMap(test)) + + println(toFM2("qwe").filterMap2(c => Some(c))) + println(toFM2(Array(2, 0)).filterMap2(c => Some(c.toInt)).toList) + //No implicit view available from java.lang.String => scala.collection.TraversableLike[A,java.lang.String]. :( + //Not anymore :) + println("qwe".filterMap2(c => Some(c))) +} diff --git a/test/files/run/t3346f.check b/test/files/run/t3346f.check new file mode 100644 index 0000000000..fd3c81a4d7 --- /dev/null +++ b/test/files/run/t3346f.check @@ -0,0 +1,2 @@ +5 +5 diff --git a/test/files/run/t3346f.scala b/test/files/run/t3346f.scala new file mode 100644 index 0000000000..4799ca2ca9 --- /dev/null +++ b/test/files/run/t3346f.scala @@ -0,0 +1,15 @@ +import scala.language.implicitConversions +import scala.language.reflectiveCalls + +object Test extends App { + trait Foo[A] + implicit def fooString: Foo[String] = null + implicit def value[A](implicit foo: Foo[A]) = 5 + + println(implicitly[Int]) + + implicit def conversion[A](x: Int)(implicit foo: Foo[A]) = new { + def aMethod = 5 + } + println(1.aMethod) +} diff --git a/test/files/run/t3346g.check b/test/files/run/t3346g.check new file mode 100644 index 0000000000..ce894825e0 --- /dev/null +++ b/test/files/run/t3346g.check @@ -0,0 +1 @@ +A(3,asdf) diff --git a/test/files/run/t3346g.scala b/test/files/run/t3346g.scala new file mode 100644 index 0000000000..d7c9d79c7f --- /dev/null +++ b/test/files/run/t3346g.scala @@ -0,0 +1,9 @@ +import scala.language.implicitConversions + +case class A(b: Int, c: String) + +object Test extends App { + implicit def s2i(s: String): Int = s.length + implicit def toA[T](t: T)(implicit f: T => Int): A = A(f(t), t.toString) + println("asdf".copy(b = 3)) +}
\ No newline at end of file diff --git a/test/files/run/t3346h.check b/test/files/run/t3346h.check new file mode 100644 index 0000000000..587be6b4c3 --- /dev/null +++ b/test/files/run/t3346h.check @@ -0,0 +1 @@ +x diff --git a/test/files/run/t3346h.scala b/test/files/run/t3346h.scala new file mode 100644 index 0000000000..97ebc9380c --- /dev/null +++ b/test/files/run/t3346h.scala @@ -0,0 +1,9 @@ +import scala.language.implicitConversions + +object Test extends App { + trait Fundep[T, U] { def u(t: T): U } + class C { def y = "x" } + implicit val FundepStringC = new Fundep[String, C]{ def u(t: String) = new C } + implicit def foo[T, U](x: T)(implicit y: Fundep[T, U]): U = y.u(x) + println("x".y) +}
\ No newline at end of file diff --git a/test/files/run/t3346j.check b/test/files/run/t3346j.check new file mode 100644 index 0000000000..59e8626fc5 --- /dev/null +++ b/test/files/run/t3346j.check @@ -0,0 +1 @@ +Int diff --git a/test/files/run/t3346j.scala b/test/files/run/t3346j.scala new file mode 100644 index 0000000000..98b5a870a7 --- /dev/null +++ b/test/files/run/t3346j.scala @@ -0,0 +1,11 @@ +import scala.language.implicitConversions +import scala.language.reflectiveCalls +import scala.reflect.runtime.universe._ + +object Test extends App { + class A[T] + class B[T] + implicit def foo[T: TypeTag](a: A[T])(implicit b: B[T]) = new { def baz = typeOf[T] } + implicit def bar[T <: Int]: B[T] = new B[T]() + println(new A[Int]().baz) +}
\ No newline at end of file |