Home > Software design >  Inferred type arguments [_$1] do not conform to method type parameter bounds
Inferred type arguments [_$1] do not conform to method type parameter bounds

Time:03-04

I have a case class :

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

And i have a function which returns a collection of the above case class objects in the form of a Seq.

val anomalyCheckConfig : Seq[AnomalyCheckConfigBuilder[_]] = jobConfig.validate.get.getAnomalyCheckConfigBuilder

I am adding the objects from the above list to another method who's signature is below :

def addAnomalyCheck[S <: State[S]](
      anomalyDetectionStrategy: AnomalyDetectionStrategy,
      analyzer: Analyzer[S, Metric[Double]],
      anomalyCheckConfig: Option[AnomalyCheckConfig] = None)
    : this.type

I am doing the below operation :

anomalyCheckConfig.foreach(x=>{
            verificationSuite = verificationSuite.addAnomalyCheck(x.anomalyDetectionStrategy,x.analyzer,x.anomalyCheckConfig)
          })

Where verificationSuite is a opensource code from Deeque

Error i am getting on the above code is :

error: inferred type arguments [_$1] do not conform to method addAnomalyCheck's type parameter bounds [S <: com.amazon.deequ.analyzers.State[S]]
[ERROR]             verificationSuite = verificationSuite.addAnomalyCheck(x.anomalyDetectionStrategy,x.analyzer,x.anomalyCheckConfig)

error: type mismatch;
[ERROR]  found   : com.amazon.deequ.analyzers.Analyzer[_$1,com.amazon.deequ.metrics.Metric[Double]]
[ERROR]  required: com.amazon.deequ.analyzers.Analyzer[S,com.amazon.deequ.metrics.Metric[Double]]
[ERROR]             verificationSuite = verificationSuite.addAnomalyCheck(x.anomalyDetectionStrategy,x.analyzer,x.anomalyCheckConfig)

The code fails on compile time and scala is unable to understand the state, and i am unable to understand where is _$1 coming from. Would appreciate some inputs on thiss

CodePudding user response:

This clearly says that Scala needs you to provide an instance of 'S' which is a subtype of State class.

What you need to do is :

anomalyCheckConfig.foreach(x=>{
            verificationSuite = verificationSuite.addAnomalyCheck[S](x.anomalyDetectionStrategy, x.analyzer.asInstanceOf[Analyzer[S, Metric[Double]]], x.anomalyCheckConfig)
          })

Also you need to wrap this under a function which accepts S as a subtype of State

    def anmomaly[S <: State[S]](){
    
        anomalyCheckConfig.foreach(x=>{
                    verificationSuite = verificationSuite.addAnomalyCheck[S](x.anomalyDetectionStrategy, x.analyzer.asInstanceOf[Analyzer[S, Metric[Double]]], x.anomalyCheckConfig)
                  })
}
  • Related