-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Read
typeclass
#932
Comments
I’m not sure such a class would be very useful, because the input type is fixed to |
Don't have any other reason besides a) Haskell has it and b) it makes sense to have "an opposite of |
It also means that we can have a law for |
If we move forward with this I would prefer it return
|
I think I tend to agree with @julienrf and @adelbertc on this: I'm not yet convinced it would be a good addition to Cats. I do, in theory, like the way that it would relate to To be honest I'm not that convinced that |
One more comment: while I think Haskell is a fantastic language, I think that Cats should avoid the "because this is what Haskell does" philosophy, which I think @milessabin agrees with me on, even if we don't necessarily agree on whether Cats should gain |
I do agree that we shouldn't do what Haskell does, just because that's what Haskell does. However, I would like to try and establish the principle that type classes can be lawful jointly and shouldn't be required to be lawful singly. Given that we don't want to remove |
I agree with @adelbertc that the result type should be |
I'd also be happy to see both |
As far I know |
I agree on both counts that trait Encode[F, T] {
def encode(f: F): T
}
trait Decode[F, T] {
def decode(f: F): Option[T]
}
type Codec[A, B] = Encode[A, B] with Decode[B, A] Although they don't violate any laws like the other felines in https://github.com/non/alleycats, it might be a better place for commonly used versions: type Show[A] = Encode[A, String]
type Read[A] = Decode[String, A]
type Bytes = Iterator[Byte]
type Serialize[A] = Encode[A, Bytes]
type Deserialize[A] = Decode[Bytes, A] |
type Codec[A, B] = Encode[A, B] with Decode[B, A]
// is the same as
type Codec[A, B] = (A => B, B => Option[A]) And that looks a lot like a prism. And prisms (and all other optics) have laws, form categories, splits, etc. As far as I understand, final case class Prism[A, B](encode: A => B, decode: Kleisli[Option, B, A])
// It could be trait Prism[A, B] {...}
// Laws:
// decode(encode(x)) = Some(x)
// decode(x).map(encode) = Some(x) or None
//
// It is also a category, a split (almost an arrow),
// an invariant functor in both type parameters,
// maybe even a cartesian in both arguments (assuming that SemigroupK[Option] is defined) (?).
object Prism {
def id[A]: Codec[A, A] = Prism(identity, Kleisli(Some.apply))
implicit val instance = new Category[Prism] with Split[Prism] {
override def id[A]: Prism[A, A] = Prism.id[A]
override def compose[A, B, C](f: Prism[B, C], g: Prism[A, B]): Prism[A, C] =
Prism(
encode=f.encode compose g.encode,
decode=f.decode andThen g.decode)
override def split[A, B, C, D](f: Prism[A, B], g: Prism[C, D]): Prism[(A, C), (B, D)] =
Prism[(A, C), (B, D)](
encode=(a: A, c: C) => (f.encode(a), g.encode(c)),
decode=Split[Kleisli[Option, ?, ?]].split(f.decode, g.decode))
}
implicit def leftInvariant[X]: Invariant[Prism[?, X]] = ???
implicit def rightInvariant[X]: Invariant[Prism[X, ?]] = ???
}
def println[A](x: A)(implicit codec: Prism[A, String]): IO[Unit] = ??? I personally don't see much of a point in having |
Just quick thought: If you are going to implement Read typeclass, please don't call it Decode :) It might be just confusing for anyone coming from ScalaZ, Haskell etc. |
I think cats should adopt the same position than the one with |
@OlivierBlanvillain : What other libraries would you recommend for |
@pathikrit |
Yeah, I’m -1 on Read. |
I'm also 👎 . As a practical matter it's not useful because it doesn't compose unless you build it out into a parser combinator library (of which there are already many to choose from). And as an dual to |
While there are various points of view on this, it seems that the majority of Cats maintainers don't think that This issue has been hanging around for quite some time. In an effort to clean up some of the stale backlog, I'm going to go ahead and close this out. |
Opposite of
Show
:See also: http://eed3si9n.com/herding-cats/Read.html
The text was updated successfully, but these errors were encountered: