typelevel / cats

Lightweight, modular, and extensible library for functional programming.
https://typelevel.org/cats/
Other
5.25k stars 1.21k forks source link

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

Open tnielens opened 4 years ago

tnielens commented 4 years ago

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.

adrian-salajan commented 4 years ago

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
tnielens commented 4 years ago

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.