optics-dev / Monocle

Optics library for Scala
https://www.optics.dev/Monocle/
MIT License
1.65k stars 206 forks source link

[Focus] zoom into a common field of an enumeration #1033

Open julien-truffaut opened 3 years ago

julien-truffaut commented 3 years ago
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

kenbot commented 3 years ago

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?

julien-truffaut commented 3 years ago

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

kenbot commented 3 years ago

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

yoohaemin commented 1 year ago

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?