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/neg/names-defaults-neg.check | 7 ++- test/files/neg/t0226.check | 3 +- test/files/neg/tcpoly_infer_ticket1162.check | 4 ++ test/files/neg/tcpoly_infer_ticket1162.scala | 8 ++++ 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 +++++++++++ 10 files changed, 162 insertions(+), 3 deletions(-) create mode 100644 test/files/neg/tcpoly_infer_ticket1162.check create mode 100644 test/files/neg/tcpoly_infer_ticket1162.scala 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') diff --git a/test/files/neg/names-defaults-neg.check b/test/files/neg/names-defaults-neg.check index 057a0519d7..e47cf8c420 100644 --- a/test/files/neg/names-defaults-neg.check +++ b/test/files/neg/names-defaults-neg.check @@ -88,8 +88,11 @@ match argument types (a: Int,b: java.lang.String) and expected result type Any names-defaults-neg.scala:70: error: wrong number of arguments for : (x: Int,y: String)A1 A1() match { case A1(_) => () } ^ -names-defaults-neg.scala:77: error: inferred kinds of the type arguments (List[Int]) do not conform to the expected kinds of the type parameters (type T). -List[Int]'s type parameters do not match type T's expected parameters: class List has one type parameter, but type T has one +names-defaults-neg.scala:77: error: no type parameters for method test4: (x: T[T[List[T[X forSome { type X }]]]])T[T[List[T[X forSome { type X }]]]] exist so that it can be applied to arguments (List[Int]) + --- because --- +argument expression's type is not compatible with formal parameter type; + found : List[Int] + required: ?T[ ?T[ scala.List[?T[ X forSome { type X } ]] ] ] Error occured in an application involving default arguments. test4() ^ diff --git a/test/files/neg/t0226.check b/test/files/neg/t0226.check index af81e41a6a..e27ffbc1e1 100644 --- a/test/files/neg/t0226.check +++ b/test/files/neg/t0226.check @@ -4,7 +4,8 @@ t0226.scala:5: error: not found: type A1 t0226.scala:5: error: not found: type A1 (implicit _1: Foo[List[A1]], _2: Foo[A2]): Foo[Tuple2[List[A1], A2]] = ^ -t0226.scala:8: error: could not find implicit value for parameter rep: Test.this.Foo[((List[Char], Int), (object Nil, Int))] +t0226.scala:8: error: diverging implicit expansion for type Test.this.Foo[((List[Char], Int), (object Nil, Int))] +starting with method list2Foo in class Test foo(((List('b'), 3), (Nil, 4))) ^ three errors found diff --git a/test/files/neg/tcpoly_infer_ticket1162.check b/test/files/neg/tcpoly_infer_ticket1162.check new file mode 100644 index 0000000000..03334222c1 --- /dev/null +++ b/test/files/neg/tcpoly_infer_ticket1162.check @@ -0,0 +1,4 @@ +tcpoly_infer_ticket1162.scala:6: error: wrong number of type parameters for method apply: [A,B,F[_]]()Test.Lift[A,B,F] in object Lift + def simplify[A,B]: Expression[A,B] = Lift[A,B]() + ^ +one error found diff --git a/test/files/neg/tcpoly_infer_ticket1162.scala b/test/files/neg/tcpoly_infer_ticket1162.scala new file mode 100644 index 0000000000..0552b42a22 --- /dev/null +++ b/test/files/neg/tcpoly_infer_ticket1162.scala @@ -0,0 +1,8 @@ +object Test { + trait Expression[A,B] + + case class Lift[A,B,F[_]]() extends Expression[F[A],F[B]] + + def simplify[A,B]: Expression[A,B] = Lift[A,B]() +} + 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