Search code examples
scalascala-cats

Incorrect Show auto derivation involving NonEmptyList


This program:

package domain

import cats.{Show, derived}
import cats.data.NonEmptyList
import cats.implicits._

case class Error(code: String, message: String)

case class Errors(errors: NonEmptyList[Error])

object ShowStuffApp {

//  import NonEmptyList.catsDataShowForNonEmptyList
  
  implicit val show: Show[Errors] = derived.semiauto.show

  def main(args: Array[String]): Unit = {

    val errors = Errors(NonEmptyList.one(Error("1234", "Could not process")))

    val res = errors.show

    println(res)
  }

}

prints out:

Errors(errors = NonEmptyList(head = Error(code = 1234, message = Could not process), tail = Nil$()))

so it has auto-derived a Show instance for the NEL, rather than using the commented implicit one (which is in scope given the top-level imports); the docs at https://github.com/typelevel/kittens?tab=readme-ov-file#user-content-derive-show state that existing implicits are respected, but this does not seem to be the case here. Uncommenting the inner import has no effect. Ideas?


Solution

  • It's missing Show instance for Error class and NonEmptyList.catsDataShowForNonEmptyList is useless without it. If you add

    implicit val showErrow: Show[Error] = derived.semiauto.show 
    

    to your code the println prints the following value:

    Errors(errors = NonEmptyList(Error(code = 1234, message = Could not process)))