cats icon indicating copy to clipboard operation
cats copied to clipboard

Add instances for Nothing

Open julien-truffaut opened this issue 6 years ago • 5 comments

What do you think about adding instances for Nothing like Eq, Ord, Semigroup?

I can submit a PR

julien-truffaut avatar Apr 08 '19 16:04 julien-truffaut

Are you proposing adding them to alleycats or cats.core? I am asking because it seems to me that they have to be lawless instances right?

kailuowang avatar Apr 08 '19 16:04 kailuowang

Some precedence: https://hackage.haskell.org/package/void-0.6.1/docs/Data-Void.html

LukaJCB avatar Apr 08 '19 17:04 LukaJCB

What's the motivation? I'd be worried that those instances would increase the chances of unwanted nothings being inferred.

milessabin avatar Apr 08 '19 18:04 milessabin

@kailuowang to cats.core, I believe they are completely legit since you can't ever produce a value of typeNothing.

@milessabin mainly because it is possible and Nothing is an important part of type theory. I am aware the compiler treats Nothing a bit differently but I can't tell if it would create any issue. I can think of some contrived examples where it could be used:

def foo[A: Eq](xs: List[A], ys: List[A]): Option[A] =
  if(xs === ys) xs.headOption
  else None

foo(Nil, Nil)

julien-truffaut avatar Apr 08 '19 19:04 julien-truffaut

These can be useful for tests. E.g. in ZIO when creating an abstraction like class Foo[E, A: Eq](action: IO[E, A]), it is useful to test the cases when the action fails by passing in something like ZIO.fail("error"). Nothing pops up naturally here.

nigredo-tori avatar Jul 25 '23 08:07 nigredo-tori