refined库允许定义与给定regex匹配的细化,如Readme中所示
import eu.timepit.refined._
import eu.timepit.refined.string._
import eu.timepit.refined.api.Refined
type MyType = String Refined MatchesRegex[W.`"[0-9]+"`.T]虽然这可以很好地工作,但我们不能以这种方式定义与包含反引号的正则表达式匹配的类型,因为正如describe here一样,在literal中没有办法转义反引号
type MyType = String Refined MatchesRegex[W.`"(a|`)"`.T]
// Getting a compile-error:
// ']' expected but ')' found.那么,有没有办法定义这样的类型(例如,使用包含反引号的正则表达式的MatchesRegex )?
发布于 2019-04-07 23:29:58
一种方法是使用Scala2.13或Typelevel Scala中提供的singleton types。
对于Typelevel,您需要在build.sbt中添加/替换
scalaOrganization := "org.typelevel",
scalaVersion := "2.12.4-bin-typelevel-4", // Assuming you are using scala 2.12并且您需要添加编译器标志-Yliteral-types
scalacOptions := Seq(
..., // Other options
"-Yliteral-types"
)现在,refined类型可以简单地为:
import eu.timepit.refined._
import eu.timepit.refined.api.Refined
type MyType = String Refined MatchesRegex["""(a|`)"""]https://stackoverflow.com/questions/55560634
复制相似问题