Scala, Extend object with a generic trait
18,196
The problem is that you're shadowing the trait's type parameter with the T
on the add
and get
methods. See my answer here for more detail about the problem.
Here's the correct code:
trait Tray[T] {
val tray = ListBuffer.empty[T]
def add (t: T) = tray += t // add[T] --> add
def get: List[T] = tray.toList // get[T] --> add
}
object Test extends Tray[Int]
Note the use of extends
in the object definition—see section 5.4 of the spec for an explanation of why with
alone doesn't work here.
Related videos on Youtube
Author by
Themerius
Updated on June 04, 2022Comments
-
Themerius over 1 year
I'm using Scala and I want to extend a (singleton) object with a trait, which delivers a data structure and some methods, like this:
trait Tray[T] { val tray = ListBuffer.empty[T] def add[T] (t: T) = tray += t def get[T]: List[T] = tray.toList }
And then I'll would like to mix-in the trait into an object, like this:
object Test with Tray[Int]
But there are type mismatches in
add
andget
:Test.add(1) // ...
How can I'll get this to work? Or what is my mistake?
-
Themerius almost 11 yearsOkay, thanks. I'll think it's because scala disassemble internal all objects to functions, and this causes this shadowing problem!
-
Travis Brown almost 11 years@Themerius: I'm not sure I understand exactly what you mean, but the problem's actually pretty simple—you can introduce a new type name in a method's type parameter list that's spelled the same as an existing type name outside of the method. You can do exactly the same thing in Java.
-
Themerius almost 11 yearsSure, it's a scope thing. Therefore it's more a feature. :)