A_P
A_P

Reputation: 336

Using applyOptional in Monocle

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.

Upvotes: 0

Views: 111

Answers (1)

P. Frolov
P. Frolov

Reputation: 876

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)

Upvotes: 0

Related Questions