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:
Is there a neater way to 'promote' an explicit parameter to implicit scope without affecting the signature of the class?
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.
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.
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.
A method or constructor can have only one implicit parameter list, and it must be the last parameter list given.
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();
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With