Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with explicit parameters required by inner implicit parameter lists

Tags:

scala

implicit

I'm currently working with a codebase that requires an explicit parameter to have implicit scope for parts of its implementation:

class UsesAkka(system: ActorSystem) {
   implicit val systemImplicit = system

   // code using implicit ActorSystem ...
}

I have two questions:

  1. Is there a neater way to 'promote' an explicit parameter to implicit scope without affecting the signature of the class?

  2. Is the general recommendation to commit to always importing certain types through implicit parameter lists, like ActorSystem for an Akka application?

Semantically speaking, I feel there's a case where one type's explicit dependency may be another type's implicit dependency, but flipping the implicit switch appears to have a systemic effect on the entire codebase.

like image 653
Lawrence Wagerfield Avatar asked May 19 '14 14:05

Lawrence Wagerfield


People also ask

What is implicit parameter?

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.

What is implicit and explicit in Scala?

For example, changing an integer variable to a string variable can be done by a Scala compiler rather than calling it explicitly. When implicit keyword used in the parameter scope of the function, all the parameters are marked as implicit. Note: A method can only contain one implicit keyword.

How many implicit parameters can an instance method have?

A method or constructor can have only one implicit parameter list, and it must be the last parameter list given.

Which variable is an implicit parameter in Javascript?

Implicit Parameter - this The this parameter refers to the object that invoked the function. This is why this parameter is also known as function context. function myFunc() { 'use strict' return this; } myFunc();


1 Answers

Why don't you make systemImplicit private ?

class UsesAkka(system: ActorSystem) {
   private implicit val systemImplicit = system
// ^^^^^^^

   // ...
}

This way, you would not change the signature of UsesAkka.

like image 63
Alex Archambault Avatar answered Oct 21 '22 23:10

Alex Archambault