Scala encuentra mi conversión implícita dos veces, lo que provoca errores "implícitos ambiguos"

I'm playing around with a third party library that has a PropertyContainer interface. It lets you get/set properties in terms of java.lang.Object, but it really only supports java primitives, strings, and arrays of those things. So I'm trying to make a light wrapper over the property container class.


class Property[A <% NeoTypeEvidence[A]](val name: String) {
  def apply(pc: PropertyContainer) = pc.getProperty(name).asInstanceOf[A]
  def update(pc: PropertyContainer, value: A) = pc.setProperty(name, value)


  sealed trait NeoTypeEvidence[A]
  object AnyValTypeEvidence extends NeoTypeEvidence[AnyVal]
  object StringTypeEvidence extends NeoTypeEvidence[String]
  object AnyValArrayTypeEvidence extends NeoTypeEvidence[Array[AnyVal]]
  object StringArrayTypeEvidence extends NeoTypeEvidence[Array[String]]

  implicit def anyValToEvidence(v: AnyVal) = AnyValTypeEvidence
  implicit def stringToEvidence(s: String) = StringTypeEvidence
  implicit def anyValArrayToEvidence(v: Array[AnyVal]) = AnyValArrayTypeEvidence
  implicit def stringArrayToEvidence(s: Array[String]) = StringArrayTypeEvidence

archivo de prueba

val name = new Property[String]("name")

I would expect that to work just fine, assuming that the compiler finds the implicit StringTypeEvidence to satisfy the condition of [String <% NeoTypeEvidence[String]]. What I get instead is an error:

ambiguous implicit values:  
both method stringToEvidence in package neo of type 
  (s: String) com.dylemma.neo.package.StringTypeEvidence.type  
 and method stringToEvidence in package neo of type 
  (s: String) com.dylemma.neo.package.StringTypeEvidence.type  
match expected type 
String => com.dylemma.neo.package.NeoTypeEvidence[String]

So scala is finding my implicit conversion twice?? I've done a clean+rebuild several times but that hasn't fixed anything. Why is this happening and how can I fix it?

edit/note: I notice that this only seems to happen when I try to create a Property in the same package as where the implicits were defined. So if I do the exact same code in some.other.package and have an import for the.main.package._, it works without complaint. I still don't understand why, though.

preguntado el 10 de marzo de 12 a las 07:03

Works for me, but nevertheless you could move the implicits to NeoTypeEvidence's companion object and see if that helps. -

Is the thing in package.scala inside a package object that you forgot to mention? -

Yes the trait and implicits were inside package object neo -

1 Respuestas

My guess is that you are importing the implicit twice. Check the rules for implicit resolution here.

Most likely (albeit hard to say with your sample), you are using an explicit import while also working on the same package, where the implicit is already present by default.

respondido 02 nov., 15:14

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.