MHJ*_*MHJ 2 dsl scala implicit structural-typing
我在隐式类中有一段代码 -
implicit class Path(bSONValue: BSONValue) {
def |<[S, T <:{def value:S}] = {
bSONValue.asInstanceOf[T].value
}
}
Run Code Online (Sandbox Code Playgroud)
问题是如果我想|<在 BSONValue 之后调用方法,我需要使用.. 例如
(doc/"_id").|<[String,BSONString]
Run Code Online (Sandbox Code Playgroud)
问题是没有.scala 引发错误,因为它不允许使用中缀表示法的类型参数方法。所以我总是必须 doc/"_id"用(). 他们是否以任何方式使用类型参数方法而不使用.例如
doc/"_id"|<[String,BSONString]
Run Code Online (Sandbox Code Playgroud)
T您想从BSONValues 中取出的所有类型都可能有一个同名的伴生对象。您可以将该伴随对象用作您实际想要获得的类型的直观占位符。沿着这些路线的东西:
trait Extract[A, BComp, B] {
def extractValue(a: A): B
}
implicit class Extractable[A](a: A) {
def |<[BC, B]
(companion: BC)
(implicit e: Extract[A, BC, B])
: B = e.extractValue(a)
}
implicit def extractIntFromString
: Extract[String, Int.type, Int] = _.toInt
implicit def extractDoubleFromString
: Extract[String, Double.type, Double] = _.toDouble
val answer = "42" |< Int
val bnswer = "42.1" |< Double
Run Code Online (Sandbox Code Playgroud)
这允许您使用中缀语法,因为所有这些都是普通值。
尽管如此,仅仅因为这是可能的,并不意味着你必须这样做。例如,我不知道对|<-operator 有什么期望。许多其他人也不知道如何处理它。他们得去查查。然后他们会看到这个签名:
def |<[BC, B](companion: BC)(implicit e: Extract[A, BC, B]): B
Run Code Online (Sandbox Code Playgroud)
我可以想象绝大多数人(包括我一周后)不会立即被这个签名所启发。
也许你可以考虑更轻量级的东西:
type BSONValue = String
trait Extract[B] {
def extractValue(bsonValue: BSONValue): B
}
def extract[B](bson: BSONValue)(implicit efb: Extract[B])
: B = efb.extractValue(bson)
implicit def extractIntFromString
: Extract[Int] = _.toInt
implicit def extractDoubleFromString
: Extract[Double] = _.toDouble
val answer = extract[Int]("42")
val bnswer = extract[Double]("42.1")
println(answer)
println(bnswer)
Run Code Online (Sandbox Code Playgroud)
它的作用似乎与|<操作符大致相同,但发生的魔法要少得多。
| 归档时间: |
|
| 查看次数: |
51 次 |
| 最近记录: |