tags:

views:

94

answers:

2

In Scala, how can i add a container trait (like Traversable[Content]) to another one that extends the container (and thus has limited visibility of its content ?

For example, the code below tries to define a trait WithIter for a container requiring Traversable (of course, i have in fact other things in 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 = {}
}

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

error: class Instance needs to be abstract, since method foreach in trait GenericTraversableTemplate of type [U](f: (Container#Value) => U)Unit is not defined

Is there any simple way?

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

If you do not specify OuterClass# when speaking of an inner class, then this. (ie, instance-specific) will be assumed.

Daniel
I'm a bit confused by the construct. You cannot say: new Instance().foreach( (x : Int) => x + 1). Why would you define it like that?
Thomas Jung
This construct can be useful when you want something that acts like Int but is incompatible--for instance, if you're defining currency or units of measurement.
Rex Kerr
@Thomas: It is not _my_ construct. Given the question's definition of `WithIter`, this is the correct way of declaring `Instance`.
Daniel
Is it useful in any way?
Thomas Jung
@Thomas that's an entirely different question. :-) Suppose you have `Time` and `Distance`, both which are `Int`, and you want to make sure you can't mix them... But the problem here is one of definition. `Traversable` was parameterized with `Container`'s `Value`, which is abstract. I can define `IntInstance` and an `Int` `Value`, and `StrInstance` with a `String` `Value`. And I could define a value `a: Value`. But I cannot pass `StrInstance.a` to `IntInstance.foreach`, can I? Even though both are `Value`, they are different.
Daniel
That is the same as `trait Container[T]{val a : T}`. But new `Instance().foreach( (x : Int) => x + 1)` works not with the abstract type member but with the generic definition.
Thomas Jung
@Thomas One is a type parameter, other is an abstract type. They are not exactly the same thing, even if they are pretty close. In particular, you cannot refer to `Container#T` or `Containter.T`, because `T` is not a type, just a parameter. On the other hand, `Value`, in the example, is a type on its own.
Daniel
+2  A: 

Why do you use an abstract type? Generics are straightforward:

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)
Thomas Jung