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]
次に simple が必要な場所に遭遇した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
asを定義することもできます。+A
(SLS4.5)