summaryrefslogtreecommitdiff
path: root/test/files/run/t3346e.scala
diff options
context:
space:
mode:
authorEugene Burmako <xeno.by@gmail.com>2013-08-11 21:54:11 +0200
committerEugene Burmako <xeno.by@gmail.com>2013-10-11 13:03:49 +0200
commit210dbc7887bc42eed4154de65d0ff5f46ca5ee58 (patch)
tree535e273511e0503a743a14fad7099d9fa73ac04c /test/files/run/t3346e.scala
parentacd77803f7da7c369f4ffdc70b5eeec4a23e35ae (diff)
downloadscala-210dbc7887bc42eed4154de65d0ff5f46ca5ee58.tar.gz
scala-210dbc7887bc42eed4154de65d0ff5f46ca5ee58.tar.bz2
scala-210dbc7887bc42eed4154de65d0ff5f46ca5ee58.zip
SI-3346 implicit parameters can now guide implicit view inference
This simple patch makes it possible for implicit views to benefit from fundep-guided type inference, eliminating a nasty special case in implicit inference. Here are the changes that I had to apply to the tests (they exposed quite a number of interesting questions that I was happy to answer): 1) neg/t5845.scala now works, so I moved it to pos. That easily makes sense, because the test was just a canary to track previous state of things. 2) neg/divergent_implicit.scala, neg/t5578.scala and neg/t7519.scala changed their messages to less informative ones, because inapplicable implicit views now get disqualified early and therefore don't display their error messages to the user. This is an unfortunate but necessary byproduct of this change, and one can argue that the behavior is now completely consistent with implicit vals (that also don't explain why this or that implicit val got disqualified, but rather display a generic implicit value not found message). 3) scaladoc/implicits-chaining.scala and scaladoc/implicits-base.scala. Immediate culling of apriori inapplicable implicit views messes things up for Scaladoc, because it's interested in potentially applicable views, having infrastructure to track various constraints (type bounds, requirements for implicit parameters, etc) that guide applicability of views and then present it to the user. Therefore, when scaladoc is detected, implicit search reverts to the old behavior. 4) We still cannot have Jason's workaround to type constructor inference mentioned in comments to SI-3346, because it's not really about implicit parameters of implicit views, but about type inference flowing from the implicit parameter list to a preceding parameter list in order to affect inference of an implicit view. This is something that's still too ambitious.
Diffstat (limited to 'test/files/run/t3346e.scala')
-rw-r--r--test/files/run/t3346e.scala81
1 files changed, 81 insertions, 0 deletions
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)))
+}