Skip to content

Unexpected behavior of NonEmptyMap #3117

@vilu

Description

@vilu

I noticed that NEM seems to prefer .find on Foldable rather on the concrete type when cats.implicits._ or more concretely import cats.instances.int._ is in scope. This confused me since I had everything working, I pulled in cats.implicits._ for some other reason and this broke the compile. What is the expected order of preference here?

object TreeWithCatsImplicit extends App {
  import cats.data.NonEmptyMap
  import cats.implicits._ // import cats.instances.int._
  val nem: NonEmptyMap[Int, String] = NonEmptyMap.of(
    1 -> "one",
    2 -> "two"
  )

  {
    import scala.reflect.runtime.universe.{reify, show}
    println(show(reify { nem.find(_ == "one") }.tree))
  }
  // implicits.toFoldableOps(Test.this.nem)(NonEmptyMapImpl.catsDataInstancesForNonEmptyMap(implicits.catsKernelStdOrderForInt)).find(((x$1) => x$1.$eq$eq("one")))
}

object WithCatsImplicit extends App {
  import cats.data.NonEmptyMap
  import cats.implicits._
  val nem: NonEmptyMap[Int, String] = NonEmptyMap.of(
    1 -> "one",
    2 -> "two"
  )

  println(nem.find(_ == "one"))
  // Some(one)
}

object WithoutCatsImplicit extends App {
  import cats.data.NonEmptyMap
  import cats.kernel.Order

  implicit val order: Order[Int] = Order.fromOrdering

  val nem: NonEmptyMap[Int, String] = NonEmptyMap.of(
    1 -> "one",
    2 -> "two"
  )

  println(nem.find(_ == "one"))
  //Some((1,one))
}

scala version 2.13
cats version 2.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions