0

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.

1 Answer 1

5

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yep - a tiny but significant error! Thanks - that works perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.