Hello,
I want to have the compiler select and use a checking function automatically. To do this I tried using implicits but have an issue. For some context assume we have a type that will be used to select the correct checking function:
sealed trait AlgorithmType
object AlgorithmType {
sealed trait Unknown extends AlgorithmType
sealed trait Classification extends AlgorithmType
sealed trait BinaryClassification extends Classification
sealed trait MultiClassification extends Classification
sealed trait OneClass extends Classification
sealed trait Regression extends AlgorithmType
}
Now I define a type that expresses a checking function “tagged” with a phantiom type:
type PhantomFunk[T <: AlgorithmType] = (Double) => Either[ADWError, Double]
where ADWError
is the helper
case class ADWError(msg: String)
To test the “selection” I created the following test:
object Test1 {
type PhantomFunk[T <: AlgorithmType] = (Double) => Either[ADWError, Double]
implicit def check: PhantomFunk[MultiClassification] = (v : Double) => Right(v)
implicit def check0: PhantomFunk[BinaryClassification] = (v : Double) => Right(v)
implicit def check2: PhantomFunk[Regression] = (v : Double) => Right(v)
class Do[A <: AlgorithmType]() {
def exec0[T >: A <: A](v: Double)(implicit ev: PhantomFunk[T]): Either[ADWError, Double] = ev(v)
}
val t2: Do[BinaryClassification] = new Do[BinaryClassification]()
val r4: Either[ADWError, Double] = t2.exec0(100.0)
println(r4)
}
When I compile this I get the error>:
ambiguous implicit values:
[error] both method check in object Test1 of type => Test1.PhantomFunk[AlgorithmType.MultiClassification]
[error] and method check0 in object Test1 of type => Test1.PhantomFunk[AlgorithmType.BinaryClassification]
[error] match expected type Test1.PhantomFunk[AlgorithmType.BinaryClassification]
[error] val r4: Either[ADWError, Double] = t2.exec0(100.0)
So my question is: how do I impose that the type A
should be AlgorithmType.BinaryClassification
so that check0
is used? I have tried several variations including just using the classes A
directly and the =:=
implicit constraint. Is their any other way to make this simpler?
TIA