Monocle icon indicating copy to clipboard operation
Monocle copied to clipboard

[Focus] zoom into a common field of an enumeration

Open julien-truffaut opened this issue 4 years ago • 4 comments

sealed trait User {
  def id: Long
}

case class Reader(id: Long, isPremium: Boolean) extends User
case class Editor(id: Long, favoriteFont: String) extends User

Focus[User](_.id)
// res: Lens[User, Long]

Original issue: https://github.com/optics-dev/Monocly/issues/14

julien-truffaut avatar Feb 08 '21 11:02 julien-truffaut

Do we require a common method in the supertrait, or do we just scout around for fields with the same name & type in the case classes?

kenbot avatar Feb 24 '21 06:02 kenbot

Without common method, I don't think we can get code completion from IDE. At least, I don't see another way.

julien-truffaut avatar Feb 24 '21 07:02 julien-truffaut

One of the earlier versions of the ticket had selection over different case classes without a supertrait method, but the usecase doesn't make any sense to me. I don't see why anyone would want that, so I strongly agree there should be a supertrait method

kenbot avatar Feb 24 '21 09:02 kenbot

I wonder if it's possible to change the implementation of .focus, so doesn't directly invoke the macro but instead takes an implicit parameter for a lens (with the macro one given as default), so in the application code there's a way to at least manually provide the instances. Will it increase compile time because of all the implicit searches though?

yoohaemin avatar Jun 20 '23 06:06 yoohaemin