首页 > 解决方案 > Kotlin 1.3.61 无法解析函数文字上扩展属性的返回类型

问题描述

在下面的示例中,我编写了简单的装饰器扩展来捕获异常并返回null任何错误。问题是num1num2类型被推断为R?而不是Double?

val <R> (()->R).nothrow: (()->R?) get() = { try { invoke() } catch(ex: Throwable) { null } }

fun main() {
  val num1 = "42"::toDouble.nothrow()
  println(num1)
  val num2 = "english"::toDouble.nothrow()
  println(num2)
}

程序输出为:

42.0
null

但是当我写

num1!! + 3.14

我得到错误:

Unresolved reference. None of the following candidates is applicable because of receiver type mismatch

候选人都是现有的plus运营商。

扩展的反编译java代码nothrow如下:

@NotNull
public static final Function0 getNothrow(@NotNull Function0 $this$nothrow) {
    int $i$f$getNothrow = 0;
    Intrinsics.checkParameterIsNotNull($this$nothrow, "$this$nothrow");
    return (Function0)(new Function0($this$nothrow) {
       // $FF: synthetic field
       final Function0 $this_nothrow;

       @Nullable
       public final Object invoke() {
          Object var1;
          try {
             var1 = this.$this_nothrow.invoke();
          } catch (Throwable var3) {
             var1 = null;
          }

          return var1;
       }

       public {
          this.$this_nothrow = var1;
       }
    });
 }

这是为什么?


编辑

问题似乎在于扩展属性:

在此处输入图像描述

在此处输入图像描述

标签: javagenericskotlinextension-methods

解决方案


如果您在调用的函数周围添加括号,它是固定的:

val num1 = ("42"::toDouble.nothrow)()
println(num1)
val num2 = ("english"::toDouble.nothrow)()
println(num2)
num1!! + 3.14 // Does not fail

如果您尝试在 IDEA 中编写此代码:

"42"::toDouble()

它说:

此语法保留供将来使用;要调用引用,请将其括在括号中: (foo::bar)(args)

这就是你的代码失败的原因。您的代码只是比调用引用更棘手,因此 IDEA 无法检测到它。


推荐阅读