In scala when compiling with -Ywarn-unused
, implicit parameters are marked as never used even if they are used in implicit scope.
For example
class MyClass(implicit: ec: ExecutionContext) {
def fun = Future.successful("hi").map(_.length)
}
This is problematic when running with -Xfatal-warnings
as well.
Is there a way to hint to the compiler that these parameters are in fact used? If not, is there another way to ensure code won't compile with unused parameters and declarations?
Implicit parameters are the parameters that are passed to a function with implicit keyword in Scala, which means the values will be taken from the context in which they are called.
Scala implicit allows you to omit calling method or parameter directly. For example, you can write a function that converts int to/from string explicitly but you can ask the compiler to do the same thing for you, implicitly.
What Are Implicit Parameters? Implicit parameters are similar to regular method parameters, except they could be passed to a method silently without going through the regular parameters list. A method can define a list of implicit parameters, that is placed after the list of regular parameters.
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.
Use the -Ywarn-macros:after
flag too. It basically tells the compiler to make the unused checks after macro expansion, that usually solve the problem of unused implicits.
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