paulmdavies
paulmdavies

Reputation: 1288

How to correctly override (implement) method defined in parameterized super trait?

In Scala, I'm trying to produce a generic set of checks to run on some data.

My base trait looks something like this:

trait Check[T]
{
    def complete() : Boolean = ...

    def passed() : Boolean = ...

    def update[T]( data : T ) : Unit
}

Then I've got two sub-traits which look a bit like this:

trait CheckFoo extends Check[Foo]{ val baz = 1 }

trait CheckBar extends Check[Bar]{ val baz = 2 }

which are designed to avoid me defining baz in each Foo and Bar check.

Then I've got some actual Foo checks, e.g.

class CheckFooOne extends CheckFoo
{
    def update( data : Foo ) : Unit = ...
}

But this doesn't compile: it tells me that CheckFooOne must be abstract, as method update is not defined.

What have I done wrong? I'm sure there's some subtlety I've missed. I'm sorry if there's another question like this already, but I can't think of what to search for, and I've checked the list of similar questions without joy.

Upvotes: 0

Views: 132

Answers (1)

gourlaysama
gourlaysama

Reputation: 11280

Your update method is parametrized with a type T that happens to have the same name as the type parameter of your class.

Your trait Check is equivalent to this:

trait Check[T]
{
    def complete() : Boolean = ...

    def passed() : Boolean = ...

    def update[U]( data : U ) : Unit
}

Whereas you probably wanted this:

trait Check[T]
{
    def complete() : Boolean = ...

    def passed() : Boolean = ...

    def update( data : T ) : Unit
}

Upvotes: 5

Related Questions