Skip to content
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 andThen on all optics #964

Closed
julien-truffaut opened this issue Nov 19, 2020 · 1 comment
Closed

Add andThen on all optics #964

julien-truffaut opened this issue Nov 19, 2020 · 1 comment
Assignees

Comments

@julien-truffaut
Copy link
Member

julien-truffaut commented Nov 19, 2020

Optics and functions compose in the same way

def compose[A, B, C](f: B => C, g: A => B): A => C
def compose[A, B, C](f: Optic[A, B], g: Optic[B, C]): Optic[A, C]

You may have noticed that the parameters of optics composition are the inverse of function composition. Optics composition looks more like andThen than compose.

def andThen[A, B, C](f: A => B, g: B => C): A => C

Therefore, I propose to create a new overloaded method andThen and deprecate all thecomposeX methods later.

For example for lenses will have something like:

trait PLens[S, T, A, B] {
  def andThen[C, D](other: PLens[A, B, C, D]): PLens[S, T, C, D] = ???
  def andThen[C, D](other: PPrism[A, B, C, D]): POptional[S, T, C, D] = ???
  def andThen[C, D](other: PIso[A, B, C, D]): PLens[S, T, C, D] = ???
  // and so on
}

I propose to overload andThen instead of using andThenLens, andThenPrism for a few reasons:

  1. it is more concise
  2. users don't need to think about what type of optics they are composing
  3. the main reason to have non-overloaded compose methods is to help type inference for optics like at, index, each which I think we can get away by adding shortcuts on each optic Add shortcut for at #956

If this works well, we can deprecate all composeLens, composePrism, ... methods.

@julien-truffaut
Copy link
Member Author

I have started to work on this

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

No branches or pull requests

1 participant