如何解决方法引用中的重载歧义?

Kir*_*man 6 kotlin method-reference

假设我想将java方法分配给方法类型Log.d(String, String)的变量x,(String, String) -> Int我这样做:

val x: (String, String) -> Int = android.util.Log::d
Run Code Online (Sandbox Code Playgroud)

编译器说:

Error:(50, 56) Overload resolution ambiguity:
public open fun d(tag: kotlin.String!, msg: kotlin.String!): kotlin.Int defined in android.util.Log
public open fun d(tag: kotlin.String!, msg: kotlin.String!, tr: kotlin.Throwable!): kotlin.Int defined in android.util.Log
Run Code Online (Sandbox Code Playgroud)

显然还有第二种方法Log.d(String, String, Throwable)但是如何告诉编译器我想要哪一种?

And*_*lav 6

此处的歧义消除目前不受支持(稍后将予以支持).

作为解决方法,您可以使用lambda表达式:

{ s, s1 -> android.util.Log.d(s, s1) }
Run Code Online (Sandbox Code Playgroud)

  • @cypressious,是的,当然 (2认同)
  • 解决歧义的语法是什么? (2认同)