./spark-shell
scala> import org.slf4j.{Logger, LoggerFactory}
import org.slf4j.{Logger, LoggerFactory}
scala> val LOGGER = LoggerFactory.getLogger("testing")
LOGGER: org.slf4j.Logger = org.slf4j.impl.Log4jLoggerAdapter(testing)
scala> val v1 = "1"
v1: String = 1
scala> val v2 = "2"
v2: String = 2
这并不像我预期的那样有效:
scala> LOGGER.warn("something {}, something {}", v1, v2)
<console>:30: error: ambiguous reference to overloaded definition,
both method warn in trait Logger of type (x$1: String, x$2: Any, x$3: Any)Unit
and method warn in trait Logger of type (x$1: String, x$2: Object*)Unit
match argument types (String,String,String)
LOGGER.warn("something {}, something {}", v1, v2)
^
但是这样做:
scala> LOGGER.warn("something {}, something {}", v1, v2, "")
16/05/30 10:16:59 WARN testing: something 1, something 2
我在这里做错了吗?
答案 0 :(得分:0)
它实际上不需要空字符串,但至少需要3个参数或类型转换。实际上,有两种与公司匹配的重载方法,就是那里显示的两种方法。因此,如果你需要放置任意数量的物品而不是2,那么一切都会按预期工作。在这种特殊情况下,使用两个对象,您可以执行以下操作之一:
(x$1: String, x$2: Object*)Unit
)匹配,就像你说的那样(第三个参数将被忽略)。Any
,这样第一家公司就会匹配(通过输入LOGGER.warn("something {}, something {}", v1:Any, v2:Any)
)