我想试试Monocle库。但我找不到基本语法的帮助资源。
简而言之,我需要Map[K,V] -> A
具有光学特性的光学器件V -> A
,我该如何定义?
假设我有一些
import monocle.macros.GenLens
case class DirState(opened: Boolean)
object DirState {
val opened = GenLens[DirState](_.opened)
}
type Path = List[String]
type StateStore = Map[Path, DirState]
接下来我遇到了我需要简单的地方StateStore => StateStore
,所以我正在导入
import monocle._
import monocle.std._
import monocle.syntax._
import monocle.function._
并尝试首先定义:
def setOpened(path: Path): StateStore => StateStore =
at(path) composeLens DirState.opened set true
到达这里
模棱两可的隐含值:类型的方法和类型的
atMap
方法都 匹配预期 类型trait MapInstances
[K, V]=> monocle.function.At[Map[K,V],K,V]
atSet
trait SetInstances
[A]=> monocle.function.At[Set[A],A,Unit]
monocle.function.At[S,Path,A]
试图将我的定义更改为
def setOpened(path: Path): StateStore => StateStore =
index(path) composeLens DirState.opened set true
现在得到:
类型不匹配; found :
monocle.function.Index[Map[Path,Nothing],Path,Nothing]
(展开为)monocle.function.Index[Map[List[String],Nothing],List[String],Nothing]
required:monocle.function.Index[Map[Path,Nothing],Path,A]
(展开为)monocle.function.Index[Map[List[String],Nothing],List[String],A]
注意:
Nothing <: A
, 但trait Index
类型不变A
。您可能希望改为定义A
为+A
。(SLS 4.5)