Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala implicit parameters are marked as unused by the compiler

Tags:

scala

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?

like image 384
kag0 Avatar asked May 29 '19 01:05

kag0


People also ask

What are implicit parameters in Scala?

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.

What is implicit function in Scala?

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 is an implicit parameter?

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.

Why does Scala have Implicits?

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.


1 Answers

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.

like image 91
Luis Miguel Mejía Suárez Avatar answered Sep 20 '22 07:09

Luis Miguel Mejía Suárez