Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two different uses of implicit parameters in Scala?

Tags:

scala

implicit

(I am fairly new to Scala, hope this isn't a stupid question.)

From what I can see, declaring a parameter to a function implicit has two (related, but quite different) uses:

  1. It makes explicitly passing a corresponding argument when calling the given function optional when the compiler can find a unique suitable value to pass (in the calling scope).

  2. It makes the parameter itself a suitable value to pass to other functions with implicit parameters (when calling them from within the given function).

In code:

def someFunction(implicit someParameter: SomeClass) = {  // Note `implicit`
  ...
  // Note no argument supplied in following call;
  // possible thanks to the combination of
  // `implicit` in `someOtherFunction` (1) and
  // `implicit` in line 1 above (2)
  someOtherFunction
  ...
}

def someOtherFunction(implicit someOtherParameter: SomeClass) = {
  ...
}

implicit val someValue = new SomeClass(...)
// Note no argument supplied in following call;
// possible thanks to `implicit` (1)
someFunction

This seems somewhat strange, doesn't it? Removing implicit from line 1 would make both calls (to someFunction from some other place, and to someOtherFunction from within someFunction) not compile.

What is the rationale behind this? (Edit: I mean what is the official rationale, in case any can be found in some official Scala resource.)

And is there a way to achieve one without the other (that is to allow passing an argument to a function implicitly without allowing it to be used implicitly within that function when calling other functions, and/or to use a non-implicit parameter implicitly when calling other functions)? (Edit: I changed the question a bit. Also, to clarify, I mean whether there is a language construct to allow this - not achieving the effect by manual shadowing or similar.)

like image 875
Tom Avatar asked Dec 26 '18 11:12

Tom


People also ask

What is the use of implicit in Scala?

The implicit system in Scala allows the compiler to adjust code using a well-defined lookup mechanism. A programmer in Scala can leave out information that the compiler will attempt to infer at compile time. The Scala compiler can infer one of two situations: A method call or constructor with a missing parameter.

What are implicit and explicit parameters?

The implicit parameter in Java is the object that the method belongs to. It's passed by specifying the reference or variable of the object before the name of the method. An implicit parameter is opposite to an explicit parameter, which is passed when specifying the parameter in the parenthesis of a method call.

Is an implicit parameter to all member functions?

The this pointer is an implicit parameter to all member functions. Therefore, inside a member function, this may be used to refer to the invoking object. Friend functions do not have a this pointer, because friends are not members of a class. Only member functions have a this pointer.

What is implicit in spark?

implicits object gives implicit conversions for converting Scala objects (incl. RDDs) into a Dataset , DataFrame , Columns or supporting such conversions (through Encoders).


1 Answers

For the first question

What is the rationale behind this?

answers are likely to be opinion-based.

And is there a way to achieve one without the other?

Yes, though it's a bit trickier than I thought initially if you want to actually use the parameter:

def someFunction(implicit someParameter: SomeClass) = {
  val _someParameter = someParameter // rename to make it accessible in the inner block

  { 
    val someParameter = 0 // shadow someParameter by a non-implicit
    someOtherFunction // doesn't compile
    someOtherFunction(_someParameter) // passed explicitly
  }
}
like image 132
Alexey Romanov Avatar answered Sep 27 '22 16:09

Alexey Romanov