How to resolve an ambiguous method reference in scala

Here is the specific problem I am facing. I am using SLF4J Logger (variable type logger below)

 //After adding to a map logger debug ("Adding {} = {}", key, value) 

Here's what tells me to hover over eclipse (and the compiler).

ambiguous reference to the overloaded definition, both debugging methods in the Logger attribute of type (x $ 1: String, x $ 2: Object *) Unit and debugging method in the Logger attribute of type (x $ 1: String, x $ 2: Any, x $ 3: Any) Types element matching arguments (String, String, String)

I understand why they are ambiguous. Of course, I do not argue with the compiler :). I just want to know how experienced programmers solve this problem.

Here are the alternatives I can use

  • Create and massage, and show the definition of Object*

    logger debug ("Adding {} = {}", Array(key, value):_*)

  • Paste into Any

    logger debug ("Adding {} = {}", key.asInstanceOf[Any], value.asInstanceOf[Any])

None of the approaches are particularly attractive. Does the community have a better approach or suggestion for me?

Thank you very much!

+5
source share
2 answers

First of all, bow to @Shadowlands, @ArneClaassen and @OlgeRudenko. As mentioned in the comments, this seems like a known issue. This prevented me from trying to β€œsolve” it. The next thing to do was find a good job that didn't break Scala's idioms.

Given these limitations, I decided to go with String Interpolation , as suggested above. I also switched to scala-logging . Quote from their github / readme,

Scala Registration is a convenient and efficient SLF4J journal registration library. This is convenient because you can simply call the log methods without checking if the appropriate log level is enabled:

logger.debug (s "Some expensive message!")

This works because the Scala macros use the check-enabled-idiom function, as well as writing this more involved code:

if (logger.isDebugEnabled) logger.debug (s "Some expensive message!")

Thanks everyone! As far as I know, this is allowed. If commentators can post their answers, I will be happy to acknowledge them.

As always, feel good on the shoulders of friendly giants!

PS: I just checked that there is no execution cost for String interpolation if you use scala -logging . My verification method was raw but effective.

  log.debug{ { throw new IllegalAccessException("This should not have been called with debug off!") } s"Added Header ${name}:${headerValue}" } 

Of course, when I set my log to DEBUG , an exception is thrown as expected, but disappears when I set it to a higher level. And yes, I already deleted the IllegalAccessException part :).

+5
source

I would use

 logger.debug("Adding {} = {}", key, value: Any) 

You can also use the following:

 logger.debug("Adding {} = {}", Array(key, value):_*) 

Pay attention to :_* . If you omit these characters, it will call the Object* method containing only 1 argument, which will be an array.

+6
source

All Articles