我想要这样的代码:
package test
object Outer {
import Outer._
implicit class OuterInt(val self: Int) extends AnyVal {
def *(that: test.Outer.Inner) = that * self
def +(that: Outer.Inner) = that + self
def -(that: Inner) = that - self
}
}
class Outer {
class Widget(w: Widget) extends Inner(w) {}
class Inner(private[Outer] val widget: Widget) {
def *(that: Int) = this
def +(that: Int) = this
def -(that: Int) = this
}
}
我正在写一个DSL,我想在这里我可以写像
val i = new Inner(...)
2 * i
2*i
编译并调用
*
方法论
Inner
反对。但是,我无法得到
内部
由编译器找到。
type Inner is not a member of object test.Outer (for *)
type Inner is not a member of object test.Outer (for +)
not found: type Inner (for -)
前两条错误消息表明它在对象中而不是类中查找类型。我尝试将隐式类移到类中,但这会导致一个错误,即隐式类型不能在类中。
内部
上课?