goggles icon indicating copy to clipboard operation
goggles copied to clipboard

Automatic support for named prisms

Open kenbot opened this issue 9 years ago • 4 comments

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?

kenbot avatar Jan 24 '17 13:01 kenbot

@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]

julien-truffaut avatar Mar 15 '17 08:03 julien-truffaut

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 avatar Mar 15 '17 09:03 kenbot

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

kenbot avatar Mar 15 '17 09:03 kenbot

Fixed, thanks.

kenbot avatar Mar 15 '17 21:03 kenbot