typelevel / typelevel/cats

Unexpected behavior of NonEmptyMap

Open
#3,117 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
5.5k
Forks
1.2k
Avg merge
2d 10h
Merged PRs (30d)
5

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the NonEmptyMap.find call and the implicit scope shown in the Scala 2.13 reproducer, comparing behavior with and without cats.implicits._ or cats.instances.int._. Trace which implicit extension supplies find and review the two reported results. Done means the expected preference is established and the behavior is documented or corrected accordingly.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
backend-api-design
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.