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 as extension method to optics in Scala 2 and 3 #1110

Merged
merged 3 commits into from
Mar 5, 2021

Conversation

julien-truffaut
Copy link
Member

For Scala 2, I added extension methods in the macro module which basically do optic.andThen(GenPrism[To, CastTo]).

For Scala 3, I added the same extension methods but in the core module. I could have implemented it by forwarding it to Focus(_.as[CastTo]) but I am worried about the documentation.

I mean for Focus it makes sense to use transparent because the return type may vary but here we know it before hand. Maybe one of you know a way to do it? Admittedly, I am just copy/pasting and tweaking code but I have no good understanding of the macro APIs.

TODO in another PR, do the same for ApplyOptics.

Copy link
Collaborator

@kenbot kenbot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes should be made to Focus too:

  • The type signature of the as keyword should be changed to be consistent with [From, To <: From], which is possible now in RC1
  • The implementation of the AsGenerator in Focus should delegate to this external operator functionality, instead of using its own implementation.

implicit def toMacroFoldOps[From, To](optic: Fold[From, To]): MacroFoldOps[From, To] = new MacroFoldOps(optic)
}

class MacroPrismOps[From, To](private val optic: Prism[From, To]) extends AnyVal {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need an extension per optic type, when have a class hierarchy now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's for narrowing the output type lens.as is an Optional but getter.as is a Fold. There might be another way to do it with transparent, I am not sure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, you will note that there is not an extension per Optic type, for example, there is not one Getter, Iso, or Lens as we don't need them to get a more precise return type.

@julien-truffaut
Copy link
Member Author

@kenbot I tried to change the definition of as extension method but it broke the parser. As far as I understand, it is because the CastTo <: A constraint add an implicit ev: CastTo <:< A.

[error]    |Unexpected code structure: TypeApply(Apply(TypeApply(Select(Ident(evidence$2),as),List(TypeTree[TypeRef(ThisType(TypeRef(ThisType(TypeRef(NoPrefix,module class focus)),class FocusAsTest)),trait Food)])),List(Select(Ident(_$2),mainIngredient))),List(Ident(Apple)))

Copy link
Collaborator

@kenbot kenbot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge away, I'll send the Focus changes in a new PR

@julien-truffaut julien-truffaut merged commit 58bd6f6 into master Mar 5, 2021
@julien-truffaut julien-truffaut deleted the as-extension branch March 5, 2021 09:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants