我正在從我自己的書Advanced scala with cats
一個簡單的練習。我想使用Cartesian
和Validated
。斯卡拉與貓 - 笛卡爾+驗證
/*
this works
*/
type ValidatedString = Validated[ Vector[String], String]
Cartesian[ValidatedString].product(
"a".valid[Vector[String]],
"b".valid[Vector[String]]
)
/* this doesnt work*/
type Result[A] = Validated[List[String], A]
Cartesian[ValidatedString].product(
f(somevariable)//returns Result[String],
g(somevariable)//returns Result[Int],
).map(User.tupled) // creates an user from the returned string, int
林完全一無所知。任何提示? 即時得到:
could not find implicit value for parameter instance: cats.Cartesian[Result] Cartesian[Result].product( ^
在你的第一個例子中,你可以用'Vector []'定義'ValidatedString',而在第二個例子中,你可以用'List []'來定義它。這是真正的區別嗎? –
這是一個小問題,但是您的第一個代碼片段_doesn't_實際上並不工作,因爲'ValidatedString'不是一個類型構造函數。這將使這個問題對未來的讀者更有用,以確保您的代碼被正確描述。 –