简体   繁体   English

Cats ValidatedNec mapN with Case Class Error Types

[英]Cats ValidatedNec mapN with Case Class Error Types

I'd like to use the Cats ValidatedNec data type similarly to the example in the Cats documentation for Validated in the section Meeting applicative -- in my case, I'm parsing String s from a file, validating against an appropriate regex for the field, and then (for several fields) converting to a different data type (assuming the regex matched). 我想使用Cats ValidatedNec数据类型,类似于Cats文档中的示例,用于ValidatedMeeting applicative中 - 在我的情况下,我正在从文件中解析String s,验证字段的相应正则表达式,然后(对于几个字段)转换为不同的数据类型(假设正则表达式匹配)。 However, instead of using case object s which extend a common trait for the invalid results (as in the example), I'd like to use case class es (which extend a common trait) so I can include contextual information in the case of failure. 但是,我没有使用扩展了无效结果的公共特征的case object (如示例中所示),而是想使用case class es(扩展共同特征),因此我可以在案例中包含上下文信息。失败。 Can this be done as simply as calling all the validation methods (putting the results in a tuple), as in the validateForm example, and calling mapN ? 这可以像调用所有验证方法(将结果放在一个元组中)一样完成,就像在validateForm示例中一样,并调用mapN I'm getting conflicting errors from Intellij (from IntelliJ, it's telling me the expected and actual parameters to mapN are the same (though it is still marking it as an error); when running sbt on the command line, it doesn't resolve the mapN method. I'm using Scala 2.12.8 and cats 2.0.0-M1. Any help would be appreciated! 我从Intellij那里得到了冲突的错误(来自IntelliJ,它告诉我mapN的预期和实际参数是相同的(尽管它仍然将其标记为错误);在命令行上运行sbt时,它无法解析mapN方法。我使用Scala 2.12.8和猫2.0.0-M1。任何帮助将不胜感激!

https://gist.github.com/DmytroMitin/6cf51c3563dba62307a2a256eeb4ebb5 https://gist.github.com/DmytroMitin/6cf51c3563dba62307a2a256eeb4ebb5


"IntelliJ errors" are irrelevant. “IntelliJ错误”无关紧要。

Regarding "sbt errors", in order to make mapN work, check that you have 关于“sbt错误”,为了使mapN工作,检查你有没有

import cats.syntax.apply._

If this still doesn't work for you provide your MCVE . 如果这仍然不起作用,请提供您的MCVE

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

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