Scala: Extending inner class, without reference to outer class

我怕爱的太早我们不能终老 提交于 2019-12-10 15:17:16

问题


I can extend an inner class/trait inside the outer class or inside a class derived from the outer class. I can extend an inner class of a specific instance of an outer class as in:

class Outer
{
  class Inner{}
}

class OtherCl(val outer1: Outer)
{
  class InnA extends outer1.Inner{}
}

Note: even this seems to compile fine producing very interesting possibilities:

trait OuterA
{ trait InnerA } 

trait OuterB
{ trait InnerB }

class class2(val outerA1: OuterA, val outerB1: OuterB)
{ class Inner2 extends outerA1.InnerA with outerB1.InnerB }

But this won't compile:

class OtherCl extends Outer#Inner

As far as I can see I'm trying to extend a parametrised class where the type parameter is an instance of the outer class so something to the effect of

class OtherCl[T where T is instance of Outer] extends T.Inner

So is the anyway to extend an inner class/ trait that's inside an outer trait/class without reference to the outer trait/class?

I am not looking to instantiate the derived inner class without an instance of the outer class only declare its type.


回答1:


You can use a trait with a self-type to do something similar. Suppose for example that we have the following:

class Outer(val x: Int) {
  class Inner {
    def y = x
  }
}

And we want to add some functionality to Inner without having an Outer around:

trait MyInner { this: Outer#Inner =>
  def myDoubledY = this.y * 2
}

Now when we instantiate an Inner we can mix in MyInner:

scala> val o = new Outer(21)
o: Outer = Outer@72ee303f

scala> val i = new o.Inner with MyInner
i: o.Inner with MyInner = $anon$1@2c7e9758

scala> i.myDoubledY
res0: Int = 42

It's not exactly what you want, but close.




回答2:


Not exactly what you are looking for, but with path dependent type (available on 2.10 or on 2.9 with -Ydependent-method-types flag you can do:

class Outer { class Inner {}; def create = new Inner }
def foo[T <: Outer](x: T) = x.create

Hope that's help



来源:https://stackoverflow.com/questions/10963909/scala-extending-inner-class-without-reference-to-outer-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!