Scala:哪个隐式参数优先?在定义方法的地方声明的那个?或者调用方法的地方?

Scala: Which implicit parameter takes priority? The one declared where a method is defined? Or where the method is called?

如果在对象中定义了一个方法来接受这样的隐式参数:

object MyApp {
   implicit val implicitParameter: String = "Hello!"

   def printImplicitString()(implicit ip: String): Unit = println(ip)
}

..然后在另一个对象中调用此方法,该对象具有自己的 String 类型的隐式参数,哪个优先?

 object MyOtherApp extends App {

   implicit val implicitParamter2: String =  "World!"

   MyApp.printImplicitString()
 }

这是打印 Hello! 还是 World!?无法在 intellij 中获取到 运行,可能是因为它可能是无效代码。

方法调用附近的优先。 Scala 将从调用方法的范围开始,并向后工作。根据 Scala 文档 here and here:

First, eligible are all identifiers x that can be accessed at the point of the method call without a prefix and that denote an implicit definition or an implicit parameter.

Second, eligible are also all members of companion modules of the implicit parameter’s type that are labeled implicit

所以这段代码将打印 'World!'