问题描述
我正在尝试查看是否有找到类型W2
的方法,该类型是2种类型W
和E
的超级类型。
在我的解决方案中,E
代表错误,W
代表警告。
我要完成的是一种方法or
,如果this
失败,则运行that
并将错误移至警告类型。
这是我在做什么的简化示例。
sealed trait Validator[-I,+E,+W,+A] extends Product with Serializable
这种类型有几种情况,在这里并不是很重要,因此,我将介绍一个示例用法:
case class MyObj(coords: GeoCoords)
case class GeoCoords(lat: String,long: String)
// Getters
val getLatitude: Validator[GeoCoords,Nothing,String] = from[GeoCoords].map(_.lat)
val getLongitude: Validator[GeoCoords,String] = from[GeoCoords].map(_.long)
val parseLatitude: Validator[GeoCoords,Exception,Double] = getLatitude andThen nonEmptyString andThen convertToDouble
val parseLongitude: Validator[GeoCoords,Double] = getLongitude andThen convertToDouble
现在这是一个不好的例子,但是我想做的是因为parseLatitude
的错误类型为Exception
,所以也许我想给它一个默认值,但是仍然可以理解它失败了。我想将Exception
从E
错误参数移到W
警告参数,如下所示:
val defaultLatitude: Validator[GeoCoords,Double] = success(0)
val finalLatitude: Validator[GeoCoords,Double] = parseLatitude or defaultLatitude
但是同样,如果不是提供默认值,而是在or
之后执行的其他操作也可能失败,因此,情况也应该如此:
val otherAction: Validator[GeoCoords,Throwable,Double] = ???
val finalLatitude: Validator[GeoCoords,Double] = parseLatitude or otherAction
我已经尝试过以多种方式在or
上实现Validator
,但是每次它给我一个问题时,基本上都是将事情一直投射到Any
。
def or[I2 <: I,E2 >: E,W2 >: W,B >: A](that: Validator[I2,E2,W2,B]): Validator[I2,B] = Validator.conversion { (i: I2) =>
val aVal: Validator[Any,E,W,A] = this.run(i)
val bVal: Validator[Any,B] = that.run(i)
val a: Vector[W2] = aVal.warnings ++ bVal.warnings
// PROBLEM HERE
val b: Vector[Any] = a ++ aVal.errors
Result(
aVal.warnings ++ aVal.errors ++ bVal.warnings,bVal.value.toRight(bVal.errors)
)
}
我必须能够说W2
既是W
的超类型,也是E
的超类型,这样我才能将Vector
连接在一起并得到类型{ {1}}结束。
一个超级简化的自包含示例是:
W2
我希望case class Example[+A,+B](a: List[A],b: List[B]) {
def or[A2 >: A,B2 >: B](that: Example[A2,B2]): Example[A2,Any] = {
Example(that.a,this.a ++ this.b ++ that.a)
}
}
object stackoverflow extends App {
val example1 = Example(List(1,2),List(3,4))
val example2 = Example(List(5,6),List(7,8))
val example3 = example1 or example2
}
的输出类型是or
而不是Example[A2,B2]
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)