Type arguments do not conform to trait type parameter bounds

321 views Asked by At

I am using a library which is written by amazon in scala here

The trait goes like this :

trait Analyzer[S <: State[_], +M <: Metric[_]]

I am trying to make a case object to store some information and an instace of the above Analyzer is a part of it.

case class AnomalyCheckConfigBuilder(anomalyDetectionStrategy: AnomalyDetectionStrategy,
                                     analyzer: Analyzer[_, Metric[_]],
                                     anomalyCheckConfig: Option[AnomalyCheckConfig] = None)

I'm getting the below error :

error: type arguments [_$1,com.amazon.deequ.metrics.Metric[_]] do not conform to trait Analyzer's type parameter bounds [S <: com.amazon.deequ.analyzers.State[_],+M <: com.amazon.deequ.metrics.Metric[_]]
       case class AnomalyCheckConfigBuilder(anomalyDetectionStrategy: AnomalyDetectionStrategy,

Not sure, what's the problem here. Does anyone understand this?

1

There are 1 answers

1
AminMal On

The thing is, if you look at the definition of State trait, you will see trait State[S <: State[S]] which means the type inside the State type argument (named S), MUST be a subtype of State of that type, for instance: trait MyState extends State[MyState]. but using underscore in analyzer: Analyzer[_(this one), Metric[_]] does not assure the compiler that this type extends State of that type. to make the compile error go away, you can do:

case class AnomalyCheckConfigBuilder(anomalyDetectionStrategy: AnomalyDetectionStrategy,
                                     analyzer: Analyzer[_ <: State[_], Metric[_]],
                                     anomalyCheckConfig: Option[AnomalyCheckConfig] = None)

which is shorter form of

case class AnomalyCheckConfigBuilder[StateType <: State[_]](anomalyDetectionStrategy: AnomalyDetectionStrategy,
                                     analyzer: Analyzer[StateType, Metric[_]],
                                     anomalyCheckConfig: Option[AnomalyCheckConfig] = None)