Characteristic inheritance dependency

In Scala, how do I add a container attribute (for example, Traversable [Content]) to another that extends the container (and therefore has limited visibility of its contents?

For example, the code below tries to determine the WithIter attribute for a container that requires Traversable (of course, I have other things in the container).

import scala.collection._ trait Container { type Value } trait WithIter extends Container with immutable.Traversable[Container#Value] class Instance extends WithIter { type Value = Int def foreach[U](f : (Value) => (U)) : Unit = {} } 

The compiler (scalac 2.8.0.Beta1-RC8) finds an error:

error: class Instance must be abstract, since the foreach method in the GenericTraversableTemplate attribute of type [U] (f: (container value #) => U) Unit is not defined

Is there an easy way?

+4
source share
2 answers
 class Instance extends WithIter { type Value = Int def foreach[U](f : (Container#Value) => (U)) : Unit = {} } 

If you do not specify OuterClass# , if we talk about the inner class, then this. assumed this. (i.e. a specific instance).

+4
source

Why are you using an abstract type? Generics are simple:

 import scala.collection._ trait Container[T] {} trait WithIter[T] extends Container[T] with immutable.Traversable[T] class Instance extends WithIter[Int] { def foreach[U](f : (Int) => (U)) : Unit = {println(f(1))} } new Instance().foreach( (x : Int) => x + 1) 
+2
source

All Articles