Kotlin为什么无法重载条件控制方法?

huangapple go评论66阅读模式
英文:

kotlin Why can't conditional control overload methods?

问题

我认为这在理论上应该有效,并且我花了很多时间来做到这一点。

这是我的代码示例。

//代码示例
fun sample(s1:String){
    //...
}

fun sample(i1:Int){
    //...
}

// 我想要...

val str = "你好"
val i = 00865

sample(if (str != "你好") i else str)
英文:

I think this should work in theory, and I've spent a lot of time doing so.

Here is an example of my code.

//code sample
fun sample(s1:String){
    //...
}

fun sample(i1:Int){
    //...
}

// I want to...

val str = "Hello"
val i = 00865

sample(if (str != "Hello") i else str)

答案1

得分: 2

确定调用的方法是在代码编译时确定的。您正在尝试在运行时确定该方法。

在您的代码中,由于这两种类型不兼容(String 和 Int),编译器会假定类型为 Any,因此没有有效的方法可供调用,因此它不会编译。

以下示例可能会使其更清楚:

fun sample(n: Int) {
    println("n is Int")
}

fun sample(n: Number) {
    println("n is Number")
}

fun callSample() {
    val a: Int = 4
    val b: Number = a

    sample(a)
    sample(b)
}

即使相同的数字(4)传递给这两种方法,输出也将是:

4 is Int
4 is Number

因为编译器使用变量的类型来确定调用的方法。

英文:

Which method is called is determined when the code is compiled. You are trying to determine the method at runtime.

In your code since the two types are incompatible (String and Int) the compile assumes that the type is Any - hence there is no valid method for it to call - so it won't compile.

The following example may make it clearer:

fun sample(n: Int) {
    println("$n is Int")
}

fun sample(n: Number) {
    println("$n is Number")
}

fun callSample() {
    val a: Int = 4
    val b: Number = a

    sample(a)
    sample(b)
}

Even though the same number (4) is passed to both methods the output will be:

4 is Int
4 is Number

Because the compiler uses the type of the variable to determine the method.

答案2

得分: 0

Kotlin是一种静态类型语言,即每个表达式在编译时必须评估为特定类型(仅有一个)。

> sample(if (str != "Hello") i else str)

在这里,条件表达式是Kotlin中的一个表达式,它应该产生一个单一的类型,但是如何确定呢?

  • if 分支返回一个整数 (i),
  • 否则分支返回一个字符串 (str)。

因此,这是两种不同的类型;但是编译器足够智能,将两者都视为 Any 类型,并给出警告:

> "条件分支的结果类型为Int/String,隐式转换为Any"

请注意:每个Kotlin类都将Any作为超类。

但是,这也不会编译通过,因为您没有一个接受 Any 类型参数的 sample 方法。

修复这个问题的一种方法是拥有重载版本:

fun sample(a: Any){
   //...
}
英文:

Kotlin is a statically typed language; i.e., each expression must evaluate to a particular type at compile time (one and only one).

> sample(if (str != "Hello") i else str)

Here, the conditional is an Expression in Kotlin, it should yield to a single type, but How?

  • The if branch returns an int (i),
  • The else branch returns a String (str).

So, these are two different types; but the compiler is smart enough to consider both as of type Any giving you the warning:

> "Conditional branch result of type Int/String is implicitly cast to Any"

Note that: Every Kotlin class has Any as a super class.

But, that won't compile as well, because you don't have a sample method that takes in Any type.

One way to fix this to have that overload version:

 fun sample(a: Any){
   //...
 }

huangapple
  • 本文由 发表于 2023年5月22日 06:49:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定