Using applyOptional in Monocle

122 Views Asked by At

I'm trying to figure out how to use applyOptional. I've got this:

import monocle.function.all.index
import monocle.macros.{GenLens, Lenses}
import monocle.std.map._
import monocle.syntax.ApplyOptionalOps._
import monocle.function.Index._

val map: Map[Int, String] = Map.empty
val lens = map applyOptional index(4)

But the compiler tells me "Cannot resolve symbol applyOptional." I imported ApplyOptionalOps._ just to confirm that I had the right imports.

1

There are 1 best solutions below

1
On

ApplyOptionalOps is the case class with the source object as the parameter, so by importing its companion object one can't access its functions. One should import monocle.syntax.apply._ instead, which extends ApplySyntax trait containing implicit conversion from generic source object to ApplyOptionalOps as well as some other operation wrappers. In fact, for just this example it the following imports is sufficient:

import monocle.syntax.apply._
import monocle.function.Index._

val map: Map[Int, String] = Map.empty
val lens = map applyOptional index(4)