What is the correct way to name an unused but required method parameter?
def validateCompatibility(em:EarthMap):Unit = {}
For this particular abstract class the validateCompatibility method has nothing to do, so the em parameter is unused. However, subclasses are free to override the method and write some assertion code for this method.
This declaration triggers a compiler warning, or at least a warning from IntelliJ about unused variable em. However, if I rename the variable to _ I now have compiler errors.
def validateCompatibility(_:EarthMap):Unit = {}
')' expected
Definition or declaration expected
';' or newline expected
Definition or declaration expected
Reassignment to val
When I insert a comment on the previous line. The warning goes away, but I’m not sure what this comment is doing? Is it telling IntelliJ to emit no warnings for unused symbols for the next line?
enable the Scala compiler unused warnings (e.g. -Wunused:params for unused parameters, see Scala Compiler Options | Scala Documentation for other options - personally I tend to enable all the unused warnings and a bunch of other -W and -Xlint options, but to each their own)
use @nowarn("cat=unused") in case of a false-positive warning (like your example)
disable that Intellij inspection in the Intellij preferences if it bothers you, since it’s redundant with the compiler warning anyway
It would be nice to use syntax like case (name, country, _city) => (name,country) just to remind the human being reading the code that, oh we’re just stripping out the city.
In my opinion the code reads easier than case (name, country, _) => (name,country)
It was a similar proposal in the contributors room.
I kind of like that, but I liked even more a proposal where one could decide which fields to extract, since that would also solve another problems.
Sadly (for me) neither one got too much traction.
It doesn’t make much sense for a lambda functions I guess. If you don’t use the parameters, you could simply replace them with _:
(_, _) => ()
The unused annotation is mostly useful for overridden methods that need to match its base’s prototype when you don’t need all the parameters given. For lambda functions you don’t need to name the parameters in the first place.
Thanks for the explanation. I think I understand, but it sounds like a strange restriction.
As I recall it is illegal to use _ as a function parameter if the function is not anonymous. Right?
Both of the following trigger a compilation error. This is why I was thinking I could not use _ as a function parameter.
@jimka I see an earlier post asked about patterns (not methods or functions).
The magic syntax is case X(foo @ _, bar @ _) => which tells linting that you want the name but not use it. It is an arbitrary convention, introduced before @unused annotation. val (a: Int @unused, b) = (17, 42) doesn’t do what you hope.