From 355264f9d53c09182fe6f480319543dc914860d1 Mon Sep 17 00:00:00 2001 From: Vlad Ureche Date: Fri, 13 Apr 2012 11:55:35 +0200 Subject: Scaladoc feature that shows implicit conversions See https://github.com/VladUreche/scala/tree/feature/doc-implicits for the history. See https://scala-webapps.epfl.ch/jenkins/view/scaladoc/job/scaladoc-implicits-nightly/ for nightlies. Many thanks fly out to Adriaan for his help with implicit search! --- test/scaladoc/resources/implicits-base-res.scala | 143 +++++++++++++++++++++ .../resources/implicits-chaining-res.scala | 48 +++++++ .../resources/implicits-elimination-res.scala | 9 ++ test/scaladoc/resources/implicits-scopes-res.scala | 51 ++++++++ 4 files changed, 251 insertions(+) create mode 100644 test/scaladoc/resources/implicits-base-res.scala create mode 100644 test/scaladoc/resources/implicits-chaining-res.scala create mode 100644 test/scaladoc/resources/implicits-elimination-res.scala create mode 100644 test/scaladoc/resources/implicits-scopes-res.scala (limited to 'test/scaladoc/resources') diff --git a/test/scaladoc/resources/implicits-base-res.scala b/test/scaladoc/resources/implicits-base-res.scala new file mode 100644 index 0000000000..db7ca4fa51 --- /dev/null +++ b/test/scaladoc/resources/implicits-base-res.scala @@ -0,0 +1,143 @@ +/** + * Test scaladoc implicits - the bread and butter of the testsuite :) + */ +package scala.test.scaladoc.implicits.base + +class Foo[T] +class Bar[T] +trait MyNumeric[R] + +/** Class A + * - tests the complete type inference + * - the following inherited methods should appear: + * {{{ + * def convToGtColonDoubleA: Double // pimpA3: with a constraint that T <: Double + * def convToIntA: Int // pimpA2: with a constraint that T = Int + * def convToManifestA: T // pimpA7: with 2 constraints: T: Manifest and T <: Double + * def convToMyNumericA: T // pimpA6: with a constraint that there is x: MyNumeric[T] implicit in scope + * def convToNumericA: T // pimpA1: with a constraint that there is x: Numeric[T] implicit in scope + * def convToPimpedA: Bar[Foo[T]] // pimpA5: no constraints + * def convToPimpedA: S // pimpA4: with 3 constraints: T = Foo[Bar[S]], S: Foo and S: Bar + * def convToTraversableOps: T // pimpA7: with 2 constraints: T: Manifest and T <: Double + * // should not be abstract! + * }}} + */ +class A[T] { + /** This should prevent the implicitly inherited `def convToPimpedA: T` from `pimpA0` from showing up */ + def convToPimpedA: T = sys.error("Let's check it out!") +} +/** Companion object with implicit transformations */ +object A { + implicit def pimpA0[V](a: A[V]) = new PimpedA(a) + implicit def pimpA1[ZBUR: Numeric](a: A[ZBUR]) = new NumericA[ZBUR](a) + implicit def pimpA2(a: A[Int]) = new IntA(a) + implicit def pimpA3(a: A[T] forSome { type T <: Double }) = new GtColonDoubleA(a) + implicit def pimpA4[S](a: A[Foo[Bar[S]]])(implicit foo: Foo[S], bar: Bar[S]): PimpedA[S] = sys.error("not implemented") + implicit def pimpA5[Z](a: A[Z]): PimpedA[Bar[Foo[Z]]] = sys.error("not implemented") + implicit def pimpA6[Z: MyNumeric](a: A[Z]) = new MyNumericA[Z](a) + // TODO: Add H <: Double and see why it crashes for C and D -- context bounds, need to check! + implicit def pimpA7[H <: Double : Manifest](a: A[H]) = new ManifestA[H](a) with MyTraversableOps[H] { def convToTraversableOps: H = sys.error("no") } +} + + +/** Class B + * - tests the existential type solving + * - the following inherited methods should appear: + * {{{ + * def convToGtColonDoubleA: Double // pimpA3: no constraints + * def convToManifestA: Double // pimpA7: no constraints + * def convToMyNumericA: Double // pimpA6: (if showAll is set) with a constraint that there is x: MyNumeric[Double] implicit in scope + * def convToNumericA: Double // pimpA1: no constraintsd + * def convToPimpedA: Bar[Foo[Double]] // pimpA5: no constraints + * def convToTraversableOps: Double // pimpA7: no constraints + * // should not be abstract! + * }}} + */ +class B extends A[Double] +object B extends A + + +/** Class C + * - tests asSeenFrom + * - the following inherited methods should appear: + * {{{ + * def convToIntA: Int // pimpA2: no constraints + * def convToMyNumericA: Int // pimpA6: (if showAll is set) with a constraint that there is x: MyNumeric[Int] implicit in scope + * def convToNumericA: Int // pimpA1: no constraints + * def convToPimpedA: Bar[Foo[Int]] // pimpA5: no constraints + * }}} + */ +class C extends A[Int] +object C extends A + + +/** Class D + * - tests implicit elimination + * - the following inherited methods should appear: + * {{{ + * def convToMyNumericA: String // pimpA6: (if showAll is set) with a constraint that there is x: MyNumeric[String] implicit in scope + * def convToNumericA: String // pimpA1: (if showAll is set) with a constraint that there is x: Numeric[String] implicit in scope + * def convToPimpedA: Bar[Foo[String]] // pimpA5: no constraints + * }}} + */ +class D extends A[String] +/** Companion object with implicit transformations */ +object D extends A + + +/** PimpedA class
+ * - tests simple inheritance and asSeenFrom + * - A, B and C should be implicitly converted to this */ +class PimpedA[V](a: A[V]) { + /** The convToPimpedA: V documentation... */ + def convToPimpedA: V = sys.error("Not implemented") +} + +/** NumericA class
+ * - tests the implicit conversion between parametric and fixed types + * - A, B and C should be implicitly converted to this */ +class NumericA[U: Numeric](a: A[U]) { + /** The convToNumericA: U documentation... */ + def convToNumericA: U = implicitly[Numeric[U]].zero +} + +/** IntA class
+ * - tests the interaction between implicit conversion and specific types + * - A and C should be implicitly converted to this */ +class IntA(a: A[Int]) { + /** The convToIntA: Int documentation... */ + def convToIntA: Int = 0 +} + +/** GtColonDoubleA class
+ * - tests the interaction between implicit conversion and existential types + * - A and B should be implicitly converted to this */ +class GtColonDoubleA(a: A[T] forSome { type T <: Double }) { + /** The convToGtColonDoubleA: Double documentation... */ + def convToGtColonDoubleA: Double = 0 +} + +/** MyNumericA class
+ * - tests the implicit conversion between parametric and fixed types + * - A should be implicitly converted to this */ +class MyNumericA[U: MyNumeric](a: A[U]) { + /** The convToMyNumericA: U documentation... */ + def convToMyNumericA: U = sys.error("dunno") +} + +/** ManifestA class
+ * - tests the manifest recognition + * - A, B, C, D should be implicitly converted to this */ +class ManifestA[W: Manifest](a: A[W]) { + /** The convToManifestA: W documentation... */ + def convToManifestA: W = sys.error("dunno") +} + +/** MyTraversableOps class
+ * - checks if any abstract members are added - should not happen! + */ +trait MyTraversableOps[S] { + /** The convToTraversableOps: S documentation... */ + def convToTraversableOps: S +} + diff --git a/test/scaladoc/resources/implicits-chaining-res.scala b/test/scaladoc/resources/implicits-chaining-res.scala new file mode 100644 index 0000000000..b20c8f846c --- /dev/null +++ b/test/scaladoc/resources/implicits-chaining-res.scala @@ -0,0 +1,48 @@ +/** + * Testing scaladoc implicits chaining + */ +package scala.test.scaladoc.implicits { + + // the classes involved + case class Z[U](a: U) + case class Intermediate[T, U](t: T, u: U) + class Implicit1[T](b: Implicit2[T]) + class Implicit2[T](c: Implicit3[T]) + class Implicit3[T](/* and so on */) + + object chaining { + + // the base conversion + implicit def convertToZ[T](a: A[T])(implicit b: Implicit1[T]): Z[A[T]] = Z(a) + + // and the implicit chaining, don't you just love it? :D + // implicit1, with one alternative + implicit def implicit1[T <: Intermediate[_, _]](implicit b: Implicit2[T]) = new Implicit1[T](b) + // implicit2, with two alternatives + implicit def implicit2alt1[T <: Intermediate[_ <: String, _]](implicit c: Implicit3[T]) = new Implicit2[T](c) + implicit def implicit2alt2[T <: Intermediate[_ <: Double, _]](implicit c: Implicit3[T]) = new Implicit2[T](c) + // implicit3, with two alternatives + implicit def implicit3alt1[T <: Intermediate[_, _ <: Int]] = new Implicit3[T]() + implicit def implicit3alt2[T <: Intermediate[_ <: Double, _ <: AnyRef],X] = new Implicit3[T]() + + // and our targets + /** conversion here, with constraints */ + class A[T]() + /** conversion here, no constraints */ + class B extends A[Intermediate[String, Int]] + /** no conversion */ + class C extends A[Intermediate[String, String]] + /** conversion here, no constraints */ + class D extends A[Intermediate[Double, Int]] + /** conversion here, no constraints */ + class E extends A[Intermediate[Double, String]] + /** no conversion */ + class F extends A[Intermediate[String, Double]] + + object scalacTest { + (new B).a + (new D).a + (new E).a + } + } +} diff --git a/test/scaladoc/resources/implicits-elimination-res.scala b/test/scaladoc/resources/implicits-elimination-res.scala new file mode 100644 index 0000000000..68743aee06 --- /dev/null +++ b/test/scaladoc/resources/implicits-elimination-res.scala @@ -0,0 +1,9 @@ +/** + * Testing scaladoc implicits elimination + */ +package scala.test.scaladoc.implicits.elimination { + /** No conversion, as B doesn't bring any member */ + class A + class B { class C; trait V; type T; } + object A { implicit def toB(a: A): B = null } +} diff --git a/test/scaladoc/resources/implicits-scopes-res.scala b/test/scaladoc/resources/implicits-scopes-res.scala new file mode 100644 index 0000000000..4e55c3e388 --- /dev/null +++ b/test/scaladoc/resources/implicits-scopes-res.scala @@ -0,0 +1,51 @@ +/** + * Testing scaladoc implicit scopes - looking for implicits in the right places + */ +package scala.test.scaladoc.implicits.scopes + +// TEST1 - In package object +package object test1 { + implicit def toB(a: A): B = null +} +package test1 { + class A + class B { def b = "" } +} + +// TEST2 - In enclosing package - doesn't seem to work even in scalac +package object test2 { + import classes._ + implicit def toB(a: A): B = null +} +package test2 { + package classes { + class A + class B { def b = "" } + object test { /* (new A).b won't compile */ } + } +} + +// TEST3 - In companion object +package test3 { + class A + object A { implicit def toB(a: A): B = null } + class B { def b = "" } +} + +// TEST4 - Nested type's companion object +package test4 { + class U[V] + class S + object S { implicit def toB(a: A): B = null } + class A extends U[S] + class B { def b = "" } +} + +// TEST5 - In scope +package test5 { + object scope { + class A + class B { def b = "" } + implicit def toB(a: A): B = null + } +} -- cgit v1.2.3