简体   繁体   English

在Monocle中使用applyOptional

[英]Using applyOptional in Monocle

I'm trying to figure out how to use applyOptional. 我试图弄清楚如何使用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." 但是编译器告诉我“无法解析符号applyOptional”。 I imported ApplyOptionalOps._ just to confirm that I had the right imports. 我导入ApplyOptionalOps._只是为了确认我的导入正确。

ApplyOptionalOps is the case class with the source object as the parameter, so by importing its companion object one can't access its functions. ApplyOptionalOps是带有源对象作为参数的case类,因此,通过导入其伴随对象将无法访问其功能。 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. 应该import monocle.syntax.apply._ ,这扩展了ApplySyntax特性,其中包含从通用源对象到ApplyOptionalOps以及其他一些操作包装的隐式转换。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM