我正在从我自己的书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'不是一个类型构造函数。这将使这个问题对未来的读者更有用,以确保您的代码被正确描述。 –