why do I get an "ambiguous implicits" error despite having prioritized implicits?
Asked Answered
G

1

6
[error] test.scala:31: ambiguous implicit values:
[error]  both method taggedQueryParamDecoder in trait ExternalInstances0 of type [A, T](implicit evidence$2: org.http4s.QueryParamDecoder[A])org.http4s.QueryParamDecoder[scalaz.@@[A,T]]
[error]  and method iiQueryParamDecoder in trait ExternalInstances1 of type [B](implicit ii: foo.InvariantInstances[B])org.http4s.QueryParamDecoder[B]
[error]  match expected type org.http4s.QueryParamDecoder[scalaz.@@[String,foo.tags.Social]]
[error]   implicitly[QueryParamDecoder[String @@ Social]]
[error]             ^

I import instances._; instances extends ExternalInstances1, and ExternalInstances1 extends ExternalInstances0. Because of this inheritance I would expect ExternalInstances1's members to win out over ExternalInstances0's, rather than yielding an ambiguity.

Why is this happening, and how can I fix it? Thanks.

The source is at http://scastie.org/12233, reproduced below:

/***
scalaVersion := "2.11.7"

libraryDependencies += "org.http4s" %% "http4s-core" % "0.10.0"

resolvers ++= Seq(
  "tpolecat" at "http://dl.bintray.com/tpolecat/maven",
  "Scalaz Bintray Repo" at "http://dl.bintray.com/scalaz/releases",
  Resolver.sonatypeRepo("releases")
)

libraryDependencies += "org.tpolecat" %% "doobie-core" % "0.2.2"

libraryDependencies += "com.github.alexarchambault" %% "argonaut-shapeless_6.1" % "0.3.1"
*/

import java.time.LocalDate

import argonaut._, Argonaut._
import doobie.imports._
import doobie.util.composite.Composite
import tags._
import instances._
import org.http4s._

import scala.reflect.runtime.universe.TypeTag
import scalaz._

object Main extends App {
  implicitly[QueryParamDecoder[String @@ Social]]
  println("ok")
}

object tags {
  trait Social; val Social = Tag.of[Social]
}

object instances extends ExternalInstances1 {
  implicit val ssn: InvariantInstances[String @@ Social] =
    InvariantInstances { (raw: String) ⇒
      val digitsOnly = raw.filter(_.isDigit)
      require(digitsOnly.length == 9)
      Social(digitsOnly)
    }(Social.unwrap)
}

trait ExternalInstances1 extends ExternalInstances0 {
  implicit def iiCodecJson[B](implicit ii: InvariantInstances[B]): CodecJson[B] = ii.codecJson
  implicit def iiEncodeJson[B](implicit ii: InvariantInstances[B]): EncodeJson[B] = ii.encodeJson
  implicit def iiDecodeJson[B](implicit ii: InvariantInstances[B]): DecodeJson[B] = ii.decodeJson
  implicit def iiMeta[B](implicit ii: InvariantInstances[B]): Meta[B] = ii.meta
  implicit def iiQueryParamEncoder[B](implicit ii: InvariantInstances[B]): QueryParamEncoder[B] = ii.queryParamEncoder
  implicit def iiQueryParamDecoder[B](implicit ii: InvariantInstances[B]): QueryParamDecoder[B] = ii.queryParamDecoder
}

trait ExternalInstances0 {
  implicit def taggedEncodeJson[A, T](implicit A: EncodeJson[A]): EncodeJson[A @@ T] =
    A.contramap(Tag.of[T].unwrap)

  implicit def taggedDecodeJson[A, T](implicit A: DecodeJson[A]): DecodeJson[A @@ T] =
    A.map(Tag.of[T].apply)

  implicit def taggedComposite[A: Composite, T]: Composite[A @@ T] =
    Composite[A].xmap(a => Tag[A, T](a), Tag.unwrap(_))

  implicit def taggedQueryParamDecoder[A: QueryParamDecoder, T]: QueryParamDecoder[A @@ T] =
    QueryParamDecoder.decodeBy(Tag.of[T](_: A))

}

trait InvariantInstances[B] {
  def codecJson: CodecJson[B]
  def decodeJson: DecodeJson[B]
  def encodeJson: EncodeJson[B]
  def meta: Meta[B]
  def queryParamEncoder: QueryParamEncoder[B]
  def queryParamDecoder: QueryParamDecoder[B]
}

object InvariantInstances {
  def apply[A: EncodeJson: DecodeJson: Meta: QueryParamDecoder: QueryParamEncoder, B: TypeTag](f: A ⇒ B)(g: B ⇒ A) =
    new InvariantInstances[B] {
      def codecJson: CodecJson[B] =
        CodecJson.derived[B](encodeJson, decodeJson)

      def decodeJson: DecodeJson[B] =
        implicitly[DecodeJson[A]].map(f)

      def encodeJson: EncodeJson[B] =
        implicitly[EncodeJson[A]].contramap(g)

      def meta: Meta[B] =
        implicitly[Meta[A]].xmap(f, g)

      def queryParamDecoder: QueryParamDecoder[B] =
        CovariantInstances.queryParamDecoder(f)

      def queryParamEncoder: QueryParamEncoder[B] =
        ContravariantInstances.queryParamEncoder(g)
    }
}

object CovariantInstances {
  def queryParamDecoder[A, B](f: A ⇒ B)(implicit A: QueryParamDecoder[A],
                                        B: reflect.runtime.universe.TypeTag[B]): QueryParamDecoder[B] =
    new QueryParamDecoder[B] {
      import scalaz.Validation.FlatMap._ // suppress deprecation warning

      def decode(value: QueryParameterValue): ValidationNel[ParseFailure, B] =
        A.decode(value).flatMap(
          a ⇒ Validation.fromTryCatchNonFatal(f(a)).leftMap(t =>
            ParseFailure(s"Query decoding ${B.tpe.typeSymbol} failed", t.getMessage)
          ).toValidationNel
        )
    }
}

object ContravariantInstances {
  def queryParamEncoder[A, B](g: B ⇒ A)(implicit A: QueryParamEncoder[A]): QueryParamEncoder[B] =
    new QueryParamEncoder[B] {
      def encode(value: B): QueryParameterValue = A.encode(g(value))
    }
}
Gastelum answered 24/9, 2015 at 6:1 Comment(3)
You cannot prioritize 'implicits'. Through inheritance you can only influence initialization order. If you want the implicit of 'ExternalInstances1' to win than you should override the implicits of ExternalInstances0.Demos
@Sascha Kolberg: Yes you can, and this is a common scala idiom. See by example: #1887453Wheatworm
Hm, my bad, didn't know that. Well, as your two implicit conversions have slightly different signatures, I'd guess that the conversion with lower priority by inheritance might have a better score for it's signature and due to a freak accident they both end up with the same score. But I would not want to be the person to debug this ;)Demos
G
3

Based on Sasha Kolberg's comment and http://eed3si9n.com/revisiting-implicits-without-import-tax, I suppose I have a "more specific" definition in ExternalInstances0 (+1 point) and a "higher priority" definition in ExternalInstances1 (+1 point) leading to a tie and ambiguity.

My workaround was to add a "more specific, higher priority" definition in ExternalInstances1 (+2 points?) to break the tie, even though it is duplicating code which was basically boilerplate to begin with.

I'd love to know a better solution. Thanks!

trait ExternalInstances1 extends ExternalInstances0 {
  implicit def iiCodecJson[B](implicit ii: InvariantInstances[B]): CodecJson[B] = ii.codecJson
  implicit def iiEncodeJson[B](implicit ii: InvariantInstances[B]): EncodeJson[B] = ii.encodeJson
  implicit def iiDecodeJson[B](implicit ii: InvariantInstances[B]): DecodeJson[B] = ii.decodeJson
  implicit def iiMeta[B](implicit ii: InvariantInstances[B]): Meta[B] = ii.meta
  implicit def iiQueryParamEncoder[B](implicit ii: InvariantInstances[B]): QueryParamEncoder[B] = ii.queryParamEncoder
  implicit def iiQueryParamDecoder[B](implicit ii: InvariantInstances[B]): QueryParamDecoder[B] = ii.queryParamDecoder

  implicit def iiCodecJsonT[B,T](implicit ii: InvariantInstances[B @@ T]): CodecJson[B @@ T] = ii.codecJson
  implicit def iiEncodeJsonT[B,T](implicit ii: InvariantInstances[B @@ T]): EncodeJson[B @@ T] = ii.encodeJson
  implicit def iiDecodeJsonT[B,T](implicit ii: InvariantInstances[B @@ T]): DecodeJson[B @@ T] = ii.decodeJson
  implicit def iiMetaT[B,T](implicit ii: InvariantInstances[B @@ T]): Meta[B @@ T] = ii.meta
  implicit def iiQueryParamEncoderT[B,T](implicit ii: InvariantInstances[B @@ T]): QueryParamEncoder[B @@ T] = ii.queryParamEncoder
  implicit def iiQueryParamDecoderT[B,T](implicit ii: InvariantInstances[B @@ T]): QueryParamDecoder[B @@ T] = ii.queryParamDecoder
}
Gastelum answered 24/9, 2015 at 17:10 Comment(2)
Can't you just swap ExternalInstances0 and ExternalInstances1? Seems to work fine (both implicitly[QueryParamDecoder[String @@ Social]] implicitly[QueryParamDecoder[String]] compile correctly. Though I can only tell that it compiles fine, not that it does what you expect.Wheatworm
Right, swapping them would resolve the conflict but give ExternalInstances0 a higher priority; I want it to have a lower priority.Gastelum

© 2022 - 2024 — McMap. All rights reserved.