Raphael Roth Raphael Roth - 10 months ago 56
Scala Question

Mark existing variable as candiate for implicit method

I try to write a method which should use a variable from the surrounding scope. The problem is that I cannot access the part of the code where the variable is defined. Something like this:

object Example extends App {

val myvar = 1.0 // cannot change that code

def myMethod()(implicit value:Double) = {


This fails because
cannot find a suitable implicit for

is there a way to "mark"
as implicit after it has been defined, other than defining a new implicit variable pointing to

Background: We use Spark-Notebook where the
) is created automatically. As
is a commonly known name for this variable in the community, we would prefer not to introduce another variable name.

Answer Source

If you just don't want to have an instance of SparkContext that isn't labeled as sc you could assign it to an implicit-underscore variable like this:

implicit val _: SparkContext = sc

That would create an implicit variable of the correct type in scope for your function to use.

Alternatively you can always pass implicit variables explicitly, in your example above you could do


and that is the solution that I would use.