-
Notifications
You must be signed in to change notification settings - Fork 21
Add @ambiguiousImplicits annotation similar to @implicitNotFound #6806
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Imported From: https://issues.scala-lang.org/browse/SI-6806?orig=1 |
Aaron Novstrup (anovstrup) said: @ambiguousImplicits(msg = "An explicit type parameter is required.")
sealed trait NotNothing[T] { type U }
object NotNothing {
implicit val nothingIsNothing = new NotNothing[Nothing] { type U = Any }
implicit def notNothing[T] = new NotNothing[T] { type U = T }
} I also don't think that this issue rises to the level of "blocker", but I have no power to edit that field. |
Eric Pederson (ericacm) said (edited on Dec 12, 2012 9:24:23 PM UTC): |
Eric Pederson (ericacm) said: |
Eric Pederson (ericacm) said (edited on Dec 15, 2012 1:23:32 AM UTC): // Courtesy of Miles Sabin. See http://stackoverflow.com/a/6944070/158658
object TypesNotEqual {
trait =!=[A, B]
implicit def neq[A, B] : A =!= B = null
// This pair excludes the A =:= B case
implicit def neqAmbig1[A] : A =!= A = null
implicit def neqAmbig2[A] : A =!= A = null
} |
@puffnfresh said: |
@puffnfresh said: import reflect.macros.Context
type NotNothing[A] = macro notNothingImpl[A]
def notNothingImpl[A: c.WeakTypeTag](c: Context) = {
import c.universe._
val isNothing = weakTypeOf[A] =:= typeOf[Nothing]
if (isNothing)
c.error(NoPosition, "An explicit type parameter is required.")
TypeTree(weakTypeOf[A])
} |
@retronym said: % scala-hash origin/paradise/macros
[info] downloading http://scala-webapps.epfl.ch/artifacts/a49722990655633c2c97ddf5699adf25bc8bea76/pack.tgz ...done.
[info] scala revision from 2013-01-14 11:39:03 -0800 downloaded to /Users/jason/usr/scala-v2.11.0-M1-80-ga497229
[info] origin/par => /Users/jason/usr/scala-v2.11.0-M1-80-ga497229
Welcome to Scala version 2.11.0-20130114-113903-a497229906 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_27).
Type in expressions to have them evaluated.
Type :help for more information.
scala> class NotNothing[T]
defined class NotNothing
scala> object NotNothing {
| val NotNothingShared = new NotNothing[Any]
| }
defined object NotNothing
warning: previously defined class NotNothing is not a companion to object NotNothing.
Companions must be defined together; you may wish to use :paste mode for this.
scala> import language.experimental.macros
import language.experimental.macros
scala> def impl[A: c.WeakTypeTag](c: reflect.macros.Context): c.Expr[NotNothing[A]] = {
| import c.universe._
| val isNothing = weakTypeOf[A] =:= typeOf[Nothing]
| if (isNothing)
| c.error(NoPosition, "An explicit, non-Nothing type parameter is required.")
| reify { NotNothing.NotNothingShared.asInstanceOf[NotNothing[A]] }
| }
impl: [A](c: scala.reflect.macros.Context)(implicit evidence$1: c.WeakTypeTag[A])c.Expr[NotNothing[A]]
scala> implicit def notNothing[A]: NotNothing[A] = macro impl[A]
defined term macro notNothing: [A]=> NotNothing[A]
scala> def foo[A: NotNothing] = ()
foo: [A](implicit evidence$1: NotNothing[A])Unit
scala> foo[Int]
scala> foo[Nothing]
<console>:20: error: could not find implicit value for evidence parameter of type NotNothing[Nothing]
foo[Nothing]
^
scala> foo
<console>:20: error: could not find implicit value for evidence parameter of type NotNothing[A]
foo
^ |
Aaron Novstrup (anovstrup) said: |
Barnabás Králik (kralikba) said: |
@puffnfresh said: |
@som-snytt said: |
Ambiguous implicit values can be employed intentionally in order to enforce a compile-time constraint (see for example http://stackoverflow.com/questions/4403906/is-it-possible-in-scala-to-force-the-caller-to-specify-a-type-parameter-for-a-po, in which ambiguity is used to prevent "Nothing" from being inferred as the return type of a method).
However, the compile time error for "ambiguous implicit values" is opaque and not obviously related to the constraint being enforced. There should be a @ambiguousImplicits annotation similar to @implicitNotFound that allows you to provide a user-friendly error message in the case that an implicit search for the annotated type triggers an "ambiguous implicit values" compile error.
The text was updated successfully, but these errors were encountered: