0

In my scala app I have two abstract classes:

abstract class Definition
abstract class Evaluator[T <: Definition]

I also have some definitions and corresponding evaluators. What I'm trying to create is class that would look like this:

abstract class AbstractEvaluationContext[E <: Evaluator[D]] 

with some methods.

My question is: how can I access the type of definition that is to be evaluated by the evaluator. eg. with class:

class SomeEvaluationContext extends AbstractEvaluationContext[SomeEvaluator[SomeDefinition]]

how can I get the type SomeDefinition in my AbstractEvaluationContext class?

For example I would like to define a method:

def getDefinition(): D = ...

which return type would be the definition passed as evaluator parameter.

1 Answer 1

2

You have to define your class this way:

abstract class AbstractEvaluationContext[D <: Definition, E <: Evaluator[D]] {
  def getDefinition(): D
}

class SomeEvaluationContext extends
  AbstractEvaluationContext[SomeDefinition, SomeEvaluator[SomeDefinition]] {
  def getDefinition(): SomeDefinition = ???
}

Actually your code in question is invalid. There is no declaration of type D:

scala> abstract class AbstractEvaluationContext[E <: Evaluator[D]]
<console>:9: error: not found: type D
       abstract class AbstractEvaluationContext[E <: Evaluator[D]]
                                                               ^
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but isn't there any way to avoid passing Definition type twice? Instead of [SomeDefinition, SomeEvaluator[SomeDefinition]] I'd prefer to write [SomeEvaluator[SomeDefinition]] since SomeDefinition is already included as SomeEvaluator parameter.
@gysyky: No, you have to specify both type parameters.

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.