kenbot / goggles

Pleasant, yet principled Scala optics DSL
MIT License
195 stars 7 forks source link

Automatic support for named prisms #11

Open kenbot opened 7 years ago

kenbot commented 7 years ago

In the same way that the .name syntax automatically navigates case-class-like fields with Lenses, it should be able to navigate ADTs in sealed class hierarchies with Prisms. This should be possible with getKnownDirectSubclasses.

But using what syntax? We could use the name of the subclass, where it looks for a field by that name first, then looks for a subclass name:

val myOpt: Option[Int] = Some(4)
set"$myOpt.Some.x"' += 1
// Some(5)

Is that obvious enough to justify "you already know how to use it"? What else could it be?

julien-truffaut commented 7 years ago

@kenbot what's the get in your example? Shouldn't it be x as Some is defined like:

final case class Some[+A](x: A) extends Option[A]
kenbot commented 7 years ago

Get/set modes generate minimal getter/setter structure respectively. It just generates Getter(_.zeroArgMethod). They can get away with this because the use case is known; lens mode (or GenLens for that matter) need full lenses because the usecase is unknown.

Fwiw, per #26, lens mode will also auto-gen Isos where it can, eg single arg case classes.

kenbot commented 7 years ago

Oh wait, my example is performing a set! That's clearly wrong.

kenbot commented 7 years ago

Fixed, thanks.