From bf584e53207b837a3103a59614177ba39f06015e Mon Sep 17 00:00:00 2001 From: Adriaan Moors Date: Thu, 22 Oct 2009 17:04:20 +0000 Subject: the essence of tcpoly inference + test cases fixes to check files and removed nonapplicable test case Tuple2 impl, but commented out so that we can bootstrap whitespace... --- test/files/pos/tcpoly_infer_easy.scala | 5 +++ .../pos/tcpoly_infer_explicit_tuple_wrapper.scala | 16 +++++++ .../pos/tcpoly_infer_implicit_tuple_wrapper.scala | 18 ++++++++ test/files/pos/tcpoly_infer_ticket1864.scala | 51 ++++++++++++++++++++++ test/files/pos/tcpoly_infer_ticket474.scala | 27 ++++++++++++ test/files/pos/tcpoly_infer_ticket716.scala | 26 +++++++++++ 6 files changed, 143 insertions(+) create mode 100644 test/files/pos/tcpoly_infer_easy.scala create mode 100644 test/files/pos/tcpoly_infer_explicit_tuple_wrapper.scala create mode 100644 test/files/pos/tcpoly_infer_implicit_tuple_wrapper.scala create mode 100644 test/files/pos/tcpoly_infer_ticket1864.scala create mode 100644 test/files/pos/tcpoly_infer_ticket474.scala create mode 100644 test/files/pos/tcpoly_infer_ticket716.scala (limited to 'test/files/pos') diff --git a/test/files/pos/tcpoly_infer_easy.scala b/test/files/pos/tcpoly_infer_easy.scala new file mode 100644 index 0000000000..0f1929502c --- /dev/null +++ b/test/files/pos/tcpoly_infer_easy.scala @@ -0,0 +1,5 @@ +object Test { + def test[CC[+X] <: Iterable[X], A](xs: CC[A]): CC[A] = xs + val xs = test(List(1,2)) + val xs2: List[Int] = test(List(1,2)) +} diff --git a/test/files/pos/tcpoly_infer_explicit_tuple_wrapper.scala b/test/files/pos/tcpoly_infer_explicit_tuple_wrapper.scala new file mode 100644 index 0000000000..de31efd565 --- /dev/null +++ b/test/files/pos/tcpoly_infer_explicit_tuple_wrapper.scala @@ -0,0 +1,16 @@ +import scala.collection.generic.GenericTraversableTemplate +import scala.collection.Iterable + +class IterableOps[CC[+B] <: Iterable[B] with GenericTraversableTemplate[B, CC], A1, A2](tuple: (CC[A1], Iterable[A2])) { + def unzip: (CC[A1], CC[A2]) = error("foo") +} + +object Test { + + implicit def tupleOfIterableWrapper[CC[+B] <: Iterable[B] with GenericTraversableTemplate[B, CC], A1, A2](tuple: (CC[A1], Iterable[A2])) + = new IterableOps[CC, A1, A2](tuple) + + val t = (List(1, 2, 3), List(6, 5, 4)) + + tupleOfIterableWrapper(t) unzip +} \ No newline at end of file diff --git a/test/files/pos/tcpoly_infer_implicit_tuple_wrapper.scala b/test/files/pos/tcpoly_infer_implicit_tuple_wrapper.scala new file mode 100644 index 0000000000..3073b298de --- /dev/null +++ b/test/files/pos/tcpoly_infer_implicit_tuple_wrapper.scala @@ -0,0 +1,18 @@ +import scala.collection.generic.GenericTraversableTemplate +import scala.collection.Iterable + +class IterableOps[CC[+B] <: Iterable[B] with GenericTraversableTemplate[B, CC], A1, A2](tuple: (CC[A1], Iterable[A2])) { + def unzip: (CC[A1], CC[A2]) = error("foo") +} + +object Test { + + implicit def tupleOfIterableWrapper[CC[+B] <: Iterable[B] with GenericTraversableTemplate[B, CC], A1, A2](tuple: (CC[A1], Iterable[A2])) + = new IterableOps[CC, A1, A2](tuple) + + val t = (List(1, 2, 3), List(6, 5, 4)) + + tupleOfIterableWrapper(t) unzip + + t unzip +} \ No newline at end of file diff --git a/test/files/pos/tcpoly_infer_ticket1864.scala b/test/files/pos/tcpoly_infer_ticket1864.scala new file mode 100644 index 0000000000..587483287d --- /dev/null +++ b/test/files/pos/tcpoly_infer_ticket1864.scala @@ -0,0 +1,51 @@ +import scala.collection.mutable.{Buffer, ArrayBuffer} + +class RichBuffer[T, B[U] <: Buffer[U]](buffer: Buffer[T]) { + def mymap[S](f: T => S)(implicit rv: B[S]): B[S] = { + buffer.foreach{ e => + rv += f(e) + } + rv + } +} + +object Application { + def mymap2[T, B[U] <: Buffer[U], S](buffer: B[T], f: T => S)(implicit rv: B[S]): B[S] = { + buffer.foreach{ e => + rv += f(e) + } + rv + } + + def mymap3[T, B <: Buffer[T], S](buffer: B, f: T => T)(implicit rv: B): B = { + buffer.foreach{ e => + rv += f(e) + } + rv + } + + def mymap4[T, B[U] <: Buffer[U], S](buffer: B[T])(f: T => S) (implicit rv: B[S]): B[S] = { + buffer.foreach{ e => + rv += f(e) + } + rv + } + + + def main(args: Array[String]) { + implicit def richBuffer[T, B[U] <: Buffer[U]](buffer: B[T]): RichBuffer[T, B] = + new RichBuffer[T, B](buffer) + + implicit val rv = new ArrayBuffer[Int] + val buf = new ArrayBuffer[Int] + (1 to 5).foreach(buf += _) + buf.mymap(x => x*x) + richBuffer(buf).mymap[Int](x => x*x) + richBuffer[Int, ArrayBuffer](buf).mymap[Int](x => x*x) + mymap2(buf, (x: Int) => x*x) + mymap2[Int, ArrayBuffer, Int](buf, (x: Int) => x*x) + // mymap3(buf, x => x*x) // compiler error + mymap3(buf, (x: Int) => x*x) + mymap4(buf)(x => x*x) + } +} diff --git a/test/files/pos/tcpoly_infer_ticket474.scala b/test/files/pos/tcpoly_infer_ticket474.scala new file mode 100644 index 0000000000..8c9be4d5c4 --- /dev/null +++ b/test/files/pos/tcpoly_infer_ticket474.scala @@ -0,0 +1,27 @@ +trait Builder[C[_], T] { + def +=(x: T) + def finalise: C[T] +} + +trait Buildable[C[_]] { + def builder[T]: Builder[C,T] +} + +object Test { + + implicit object buildableList extends Buildable[List] { + def builder[T] = new Builder[List,T] { + val buf = new scala.collection.mutable.ListBuffer[T] + def +=(x: T) = buf += x + def finalise = buf.toList + } + } + + def foo[C[_],T](x: T)(implicit b: Buildable[C]): C[T] = { + val builder = b.builder[T] + builder += x + builder.finalise + } + + val l: List[Int] = foo(8) +} \ No newline at end of file diff --git a/test/files/pos/tcpoly_infer_ticket716.scala b/test/files/pos/tcpoly_infer_ticket716.scala new file mode 100644 index 0000000000..cfba07fa43 --- /dev/null +++ b/test/files/pos/tcpoly_infer_ticket716.scala @@ -0,0 +1,26 @@ + +trait Functor[F[_]] { + def fmap[A,B](fun: A=>B, arg:F[A]): F[B] +} +object Functor{ + implicit val ListFunctor: Functor[List] = new Functor[List] { + def fmap[A, B](f: A => B, arg: List[A]):List[B] = arg map f + } + + final class OOFunctor[F[_],A](arg:F[A])(implicit ftr: Functor[F]) { + def fmap[B](fun: A=>B):F[B] = ftr.fmap(fun,arg) + } + + //breaks if uncommented + implicit def lifttoOO[F[_],A](arg:F[A])(implicit ftr: Functor[F]) = new OOFunctor[F,A](arg)(ftr) + + //works if uncommented + //implicit def liftListtoOO[A](arg:List[A]):OOFunctor[List,A] = new OOFunctor[List,A](arg) +} + +object GeneralLiftingDemo extends Application { + import Functor._ + val l = List(1,2,3) + val res = l fmap( 1+) // TODO: should not need explicit call to lifttoOO + println("OO : " + res ) +} \ No newline at end of file -- cgit v1.2.3