Skip to content

Impossible for Scala 2.12 to handle overloaded Java method that contains both varargs and parameters of same type #12765

Closed as not planned
@mdedetrich

Description

@mdedetrich

Question for anyone that is still familiar with Scala 2.12 (specifically 2.12.17), I seem to have found a compiler bug that I cannot get around. Assuming that we have 2 java methods (these methods are actually from slf4j)

    /**
     * Log a message at the DEBUG level according to the specified format
     * and arguments.
     * <p/>
     * <p>This form avoids superfluous object creation when the logger
     * is disabled for the DEBUG level. </p>
     *
     * @param format the format string
     * @param arg1   the first argument
     * @param arg2   the second argument
     */
    public void debug(String format, Object arg1, Object arg2);

    /**
     * Log a message at the DEBUG level according to the specified format
     * and arguments.
     * <p/>
     * <p>This form avoids superfluous string concatenation when the logger
     * is disabled for the DEBUG level. However, this variant incurs the hidden
     * (and relatively small) cost of creating an <code>Object[]</code> before invoking the method,
     * even if this logger is disabled for DEBUG. The variants taking
     * {@link #debug(String, Object) one} and {@link #debug(String, Object, Object) two}
     * arguments exist solely in order to avoid this hidden cost.</p>
     *
     * @param format    the format string
     * @param arguments a list of 3 or more arguments
     */
    public void debug(String format, Object... arguments);

It seems to be impossible for Scala 2.12 to call either these methods. In other words no matter what you do you will always get an

[error] /Users/mdedetrich/github/incubator-pekko-persistence-r2dbc/core/src/main/scala/akka/persistence/r2dbc/internal/R2dbcExecutor.scala:306:15: ambiguous reference to overloaded definition,
[error] both method debug in trait Logger of type (x$1: String, x$2: Object*)Unit
[error] and  method debug in trait Logger of type (x$1: String, x$2: Any, x$3: Any)Unit

where as in Java it would pick the public void debug(String format, Object arg1, Object arg2) version over public void debug(String format, Object... arguments); if you only supply arg1 and arg2 (and this is even stated as such in the documentation).

I tried using various ways to force the type (i.e. : Object or .asInstanceOf[Object]) but nothing seems to help. Even

log.debug("test", new Object, new Object)

Will fail to compile.

Pinging @som-snytt because I think you have the most experience here and actually worked on similar bugs

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions