cats icon indicating copy to clipboard operation
cats copied to clipboard

alleycats - no default instance for Empty[Option[T]]

Open tnielens opened this issue 5 years ago • 2 comments

There is no default instance for Empty[Option[T]] if T has no Monoid. But there is one for collections. Would it make sense to add an Empty[Option[T]] instance that behaves like the collection default ones?

  import alleycats.Empty
  import cats.implicits._
  import java.time.LocalDateTime

  implicit val dtEmtpy = Empty(new LocalDateTime(0))

  // works fine
  Empty[List[LocalDateTime]]
  Empty[Set[LocalDateTime]]
  Empty[Stream[LocalDateTime]]

  // doesn't work by default
  Empty[Option[LocalDateTime]]

EDIT: rewrite the example with a type that has no Monoid.

tnielens avatar Sep 27 '20 15:09 tnielens

If I'm not misunderstanding something, works for me with 2.2.0

  test("empty") {
      import alleycats.Empty

      // works fine
      Empty[List[Int]]
      Empty[Set[Int]]
      Empty[Stream[Int]]

      // doesn't work by default
      println(">>>> "+ Empty[Option[Int]].empty) // >>>> None
//resolved implicit is alleycats.EmptyInstances1.monoidIsEmpty
  }

indeed it does not work if T has no Monoid

Empty[Option[T]].empty

adrian-2414745 avatar Sep 30 '20 14:09 adrian-2414745

Indeed, I got confused while trying to minimize the example. I'll rewrite the example with a type for which a Monoid wouldn't make sense.

tnielens avatar Sep 30 '20 15:09 tnielens