简而言之:我尝试为A <N B
Scala 中的 DSL 编写类似 T 类型的整数 N 和 A,B 的东西。这样做有很好的可能性吗?
Longer:我尝试在 Scala 中为Tgrep2编写 DSL。我目前有兴趣写
A <N B B is the Nth child of A (the rst child is <1).
以一种很好的方式,并尽可能接近 Scala 中的原始定义。有没有办法重载<
运算符,它可以将 N 和 B 作为参数。
我尝试了什么:我尝试了两种不同的可能性,但并没有让我很开心:
scala> val N = 10
N: Int = 10
scala> case class T(n:String) {def <(i:Int,j:T) = println("huray!")}
defined class T
scala> T("foo").<(N,T("bar"))
huray!
和
scala> case class T(n:String) {def <(i:Int) = new {def apply(j:T) = println("huray!")}}
defined class T
scala> (T("foo")<N)(T("bar"))
warning: there were 1 feature warnings; re-run with -feature for details
huray!