Skip to content

Instantly share code, notes, and snippets.

@julien-truffaut
Created June 3, 2020 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julien-truffaut/cfa7304e16608c557b170be06dbbd09e to your computer and use it in GitHub Desktop.
Save julien-truffaut/cfa7304e16608c557b170be06dbbd09e to your computer and use it in GitHub Desktop.
SIP proposal for using type alias in type inference and error messages

Hi all,

Yilin recently had an idea for a SIP proposal for Scala 3 that I think would benefit to Monocle, ZIO and probably the rest of the Scala ecosystem. It is quite a vague concept at the moment, but I thought it would be worth getting your inputs.

Problem: ZIO, fs2 and other libraries found out that a natural encoding in Scala relies on multiple type parameters with variance (I believe John called this effect rotation):

  • ZIO[-R, +E, +A]
  • fs2.Stream[F[_], +A]
  • Optic[+E, -S, +T, +A, -B] or even more type parameters if include index optics

With this encoding, we can express the absence of error (E = Nothing), absence of effect (F = Nothing), absence of environment variables (R = Any) and all with perfect type inference. So it is a big win on all fronts except for function signatures and error messages. As we can end-up with an Optic[Nothing, User, User, String, String] instead of Lens[User, String] or ZIO[Any, Nothing, Int] instead of UIO[A]. Type aliases help a little bit, but they get often lost.

The proposal: Define a set of rules for the compiler to use the most "precise" type alias in type inference and error messages.

For example, the compiler could look inside of companion object for type aliases and try them in top-to-bottom order or something more efficient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment