使用精化约束Interval.Closed

时间:2016-12-14 21:57:06

标签: scala

我发布了a similar question earlier,修复了。然而,要问这个问题,我简化了我最初的问题。但是,这个简单问题的解决方案似乎并没有解决我最初的问题

当我尝试这段简短的代码片段时,我尝试使用精炼库(https://github.com/fthomas/refined)中的Interval.Closed类型约束Double类型,从而产生编译错误。

import eu.timepit.refined._
import eu.timepit.refined.api.{Refined, Validate}
import eu.timepit.refined.auto._
import eu.timepit.refined.numeric._
import eu.timepit.refined.api.Refined
import eu.timepit.refined.numeric.Interval

object Lala {
  type UnitReal = Double Refined Interval.Closed[W.`0.0`.T, W.`1.0`.T]

  def foo(x: Double): Either[String, UnitReal] = refineV[UnitReal](x)
}

显示此编译错误:

  

错误:(13,67)找不到参数v的隐含值:   eu.timepit.refined.api.Validate [Double,xxx.Lala.UnitReal] def foo(x:   Double):[String,UnitReal] = refineVUnitReal

似乎缺少Interval.Closed类型的Validate实现。我想知道是否有人可以帮我找到Interval.Closed类型的Validate特征的实例?或者我应该自己提供这样的实例?

1 个答案:

答案 0 :(得分:0)

我能够通过分离约束和约束类型来解决问题,并使用约束来细化值(即refineVOneToZero):

object Lala {
  type OneToZero = Interval.Closed[W.`0.0`.T, W.`1.0`.T]
  type UnitReal = Double Refined OneToZero

  def foo(x: Double): Either[String, UnitReal] = refineV[OneToZero](x)
}