Monocle icon indicating copy to clipboard operation
Monocle copied to clipboard

Recommended replacement for @Lenses

Open Lermex opened this issue 4 years ago • 4 comments

Am I right to assume that using Focus at all use sites instead of lenses pre-generated with the @Lenses annotation is gonna affect compilation times in the same way e.g. using circe.auto instead of circe.semiauto does? If so, what's the recommended approach here?

Should I replace @Lenses with something like val lens = GenLens[MyClass] in the MyClass companion object?

Is there a plan to support the derives clause in Monocle?

Lermex avatar Jul 21 '21 14:07 Lermex

Rather than compile-times (it's fairly fast), you'll get code blow up since it generates an in-line method each call-site; YMMV as to whether you care or not.

You can use the Focus object or GenLens to share the optic. Your snippet is probably what I would do.

I don't understand what derives support would entail. Could you please elaborate?

yilinwei avatar Jul 22 '21 20:07 yilinwei

Presumably, you would be able to write

case class MyClass (
  fieldOne: String,
  fieldTwo: Int
) derives Lenses

and then either do MyClass.fieldOne or MyClass.lens.fieldOne to access the lens for a particular field

Lermex avatar Jul 26 '21 10:07 Lermex

@Lermex I don't think it works that way. IIUC derives Lenses would result in val lenses = Lenses.derived[MyClass] to be added to object MyClass. I don't think it has the capability to add multiple named fields to object MyClass unfortunately. (Would be awesome though!)

japgolly avatar Aug 02 '21 00:08 japgolly

FYI, https://github.com/lampepfl/dotty/pull/16454

yilinwei avatar Dec 19 '22 20:12 yilinwei