In scala, why this implicit parameter cannot be inferred from the same scope?

146 views Asked by At

I'm writing a simple Apache Spark utility that automatically creates an AccumulatorV2 based on provided initial value:

  import java.lang

  type Acc[T] = AccumulatorV2[T, T]

  implicit val long1: Long => Acc[lang.Long] = _ => new LongAccumulator()LongAccumulator()

  implicit def double1: Double => Acc[lang.Double] = _ => new DoubleAccumulator()

  def accumulator[T1, T2](
                           initialValue: T1,
                           name: String
                         )(implicit
                           canBuildFrom: T1 => Acc[T2],
                           converter: T1 => T2,
                           sc: SparkContext = SparkContext.getOrCreate()
                         ): Acc[T2] = {

    val acc: Acc[T2] = canBuildFrom(initialValue)
    acc.reset()
    acc.add(initialValue)

    sc.register(acc, name)

    acc
  }

The 2 implicit parameters: canBuildFrom and converter are supposed to be provided by implicit function and compiler predefined typecast respectively. However, when I tried to call it:

Metrics.accumulator(0L, "webDriverDispatched")

I got the following error information:

Error:(102, 84) No implicit view available from Long => com.tribbloids.spookystuff.Metrics.Acc[T2].
                          webDriverDispatched: Acc[lang.Long] = Metrics.accumulator(0L, "webDriverDispatched"),
Error:(102, 84) not enough arguments for method accumulator: (implicit canBuildFrom: Long => com.tribbloids.spookystuff.Metrics.Acc[T2], implicit converter: Long => T2, implicit sc: org.apache.spark.SparkContext)com.tribbloids.spookystuff.Metrics.Acc[T2].
Unspecified value parameters canBuildFrom, converter.
                          webDriverDispatched: Acc[lang.Long] = Metrics.accumulator(0L, "webDriverDispatched"),

Why scala compiler failed to find the right implicit value? And what should I do to fix it?

0

There are 0 answers