From 5bd8ea0edffe7b725e5fa665a82a5795d5dafe8f Mon Sep 17 00:00:00 2001 From: Brian McKenna Date: Sun, 7 Sep 2014 12:33:52 -0600 Subject: SI-6806 Add an @implicitAmbiguous annotation Example usage: trait =!=[C, D] implicit def neq[E, F] : E =!= F = null @annotation.implicitAmbiguous("Could not prove ${J} =!= ${J}") implicit def neqAmbig1[G, H, J] : J =!= J = null implicit def neqAmbig2[I] : I =!= I = null implicitly[Int =!= Int] Which gives the following error: implicit-ambiguous.scala:9: error: Could not prove Int =!= Int implicitly[Int =!= Int] ^ Better than what was previously given: implicit-ambiguous.scala:9: error: ambiguous implicit values: both method neqAmbig1 in object Test of type [G, H, J]=> Main.$anon.Test.=!=[J,J] and method neqAmbig2 in object Test of type [I]=> Main.$anon.Test.=!=[I,I] match expected type Main.$anon.Test.=!=[Int,Int] implicitly[Int =!= Int] ^ --- .../scala/annotation/implicitAmbiguous.scala | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/library/scala/annotation/implicitAmbiguous.scala (limited to 'src/library') diff --git a/src/library/scala/annotation/implicitAmbiguous.scala b/src/library/scala/annotation/implicitAmbiguous.scala new file mode 100644 index 0000000000..46eab9ae8f --- /dev/null +++ b/src/library/scala/annotation/implicitAmbiguous.scala @@ -0,0 +1,34 @@ +package scala.annotation + +import scala.annotation.meta._ + +/** + * To customize the error message that's emitted when an implicit of type + * C[T1,..., TN] is found more than once, annotate the class C + * with @implicitAmbiguous. Assuming C has type parameters X1,..., XN, the + * error message will be the result of replacing all occurrences of ${Xi} in + * the string msg with the string representation of the corresponding type + * argument Ti. * + * + * If more than one @implicitAmbiguous annotation is collected, the compiler is + * free to pick any of them to display. + * + * Nice errors can direct users to fix imports or even tell them why code + * intentionally doesn't compile. + * + * {{{ + * trait =!=[C, D] + * + * implicit def neq[E, F] : E =!= F = null + * + * @annotation.implicitAmbiguous("Could not prove ${J} =!= ${J}") + * implicit def neqAmbig1[G, H, J] : J =!= J = null + * implicit def neqAmbig2[I] : I =!= I = null + * + * implicitly[Int =!= Int] + * }}} + * + * @author Brian McKenna + * @since 2.12.0 + */ +final class implicitAmbiguous(msg: String) extends scala.annotation.StaticAnnotation {} -- cgit v1.2.3