gysyky
gysyky

Reputation: 470

Scala nested parametrized class

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.

Upvotes: 0

Views: 60

Answers (1)

senia
senia

Reputation: 38045

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]]
                                                               ^

Upvotes: 2

Related Questions