1
votes

I'm still new to context bounds in Scala and I'm trying to write a function that can perform a reduceByKey on any any generic that satisfies a context bound. eg

def myReduce[K: ClassTag, V: ClassTag: Numeric[V]](in: RDD[(K, V)]): RDD[(K, V)] = {
  in.reduceByKey{case (v1, v2) =>
    implicityly[Numeric[V]].plus(v1, v2)
  }

As far as I understand this should work however when trying to compile I get the following errors.

  1. error: could not find implicit value for parameter e: Numeric
  2. error: Numeric[V] does not take type parameters

These I don't understand. I'm not passing in type parameters to Numeric[V] anywhere. Also shouldn't the context bound automatically import the 'evidence' for the conversion?

I was hoping to instead to eventually use my own trait instead of Numeric.

Edit:

I tried explicitly writing the implicit evdience in the function as follows:

def myReduce[K: ClassTag, V: ClassTag](in: RDD[(K, V)])(implicit ev: V => Numeric[V]): RDD[(K, V)] = {
  in.reduceByKey{case (v1, v2) =>
    implicityly[Numeric[V]].plus(v1, v2)
  }

But I still get the "error: could not find implicit value for parameter e: Numeric" error.

1

1 Answers

1
votes

Ok so I solved the issue

def myReduce[K: ClassTag, V: ClassTag: Numeric](in: RDD[(K, V)]): RDD[(K, V)] = {
  in.reduceByKey{case (v1, v2) =>
    implicityly[Numeric[V]].plus(v1, v2)
  }

The issue was specifying Numeric[V] instead of just Numeric. I've ran into more issues now but that's for another day.