Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin函数语法_Kotlin - Fatal编程技术网

Kotlin函数语法

Kotlin函数语法,kotlin,Kotlin,我做Kotlin Koans测试是为了熟悉Kotlin。在某个测试中,我必须重写compareTo方法。在第一种情况下,一切都按预期进行 data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) { operator fun compareTo(other: MyDate)= when { year != other.year -> year - other.year

我做Kotlin Koans测试是为了熟悉Kotlin。在某个测试中,我必须重写
compareTo
方法。在第一种情况下,一切都按预期进行

data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) {

    operator fun compareTo(other: MyDate)= when {
        year != other.year -> year - other.year
        month != other.month -> month - other.month
        else -> dayOfMonth - other.dayOfMonth
    }

}
现在,在第二种情况下,我编写的
比较有点不同,我得到了大量的编译错误

data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) {


     operator fun compareTo(other: MyDate){

        when {
            year != other.year -> return year - other.year
            month != other.month -> return month - other.month
            else -> return dayOfMonth - other.dayOfMonth
       }
     }


 }
首先,在运算符关键字处,我得到一个错误:

“运算符”修饰符不适用于此函数:必须返回Int

在我得到的回报中

类型不匹配:推断的类型为Int,但应为Unit


我无法理解为什么会出现这些错误,因为第一个实现返回相同的
Int
s

,这是因为
=
作为
{return X}
工作。这只适用于函数定义。这意味着在第一个示例中,您的代码等于

operator fun compareTo(other: MyDate):Int {
    return when {
        year != other.year -> year - other.year
        month != other.month -> month - other.month
        else -> dayOfMonth - other.dayOfMonth
    }
}
但在第二个示例中,当
时,不返回
的结果。这会导致编译器感到困惑,因为它希望您返回
Int
,但您返回
Unit
(相当于Java的
void


因此,您需要做的就是添加一个显式的返回类型(
Int
,在本例中)到它(
funx(/*args*/)
:Int
或其他适用类型)

,这是因为
=
{return X}
一样工作,这只适用于函数定义。这意味着在第一个示例中,您的代码等于

operator fun compareTo(other: MyDate):Int {
    return when {
        year != other.year -> year - other.year
        month != other.month -> month - other.month
        else -> dayOfMonth - other.dayOfMonth
    }
}
但在第二个示例中,当
时,不返回
的结果。这会导致编译器感到困惑,因为它希望您返回
Int
,但您返回
Unit
(相当于Java的
void


因此,您所需要做的就是向其添加一个显式的返回类型(
Int
,在本例中为
funx(/*args*/)
:Int
或其他适用类型)

在第一个示例中,返回类型为
compareTo(MyDate)
Int
相同,因为
表达式的所有分支都返回
Int

在第二个示例中,
compareTo(MyDate)
的返回类型是
Unit
。由于您的函数具有块体,因此必须明确指定返回类型(除非它们打算返回
Unit
)。因此,这里的返回类型应该是
Unit
,但是当
表达式为
Int
时,从
推断出的返回类型是
Int。这就是为什么会出现错误:

类型不匹配:推断的类型为Int,但应为Unit

下面是使用块体显式定义函数返回类型的方法:

具有块体的函数必须始终明确指定返回类型,除非它们打算返回单元。Kotlin不会为具有块体的函数推断返回类型,因为这样的函数在块体中可能有复杂的控制流,并且返回类型对于读取器(有时甚至对于编译器)来说是不明显的

因此,声明compareTo(MyDate)
的正确方法是指定函数的返回类型,如果它包括块体:

operator fun compareTo(other: MyDate): Int {
    when {
        year != other.year -> return year - other.year
        month != other.month -> return month - other.month
        else -> return dayOfMonth - other.dayOfMonth
    }
}

这同时解决了另一个错误,因为需要返回
Int

在第一个示例中,
compareTo(MyDate)
的返回类型为
Int
,因为
when
表达式的所有分支都返回
Int

在第二个示例中,
compareTo(MyDate)
的返回类型是
Unit
。由于您的函数具有块体,因此必须明确指定返回类型(除非它们打算返回
Unit
)。因此,这里的返回类型应该是
Unit
,但是当
表达式为
Int
时,从
推断出的返回类型是
Int。这就是为什么会出现错误:

类型不匹配:推断的类型为Int,但应为Unit

下面是使用块体显式定义函数返回类型的方法:

具有块体的函数必须始终明确指定返回类型,除非它们打算返回单元。Kotlin不会为具有块体的函数推断返回类型,因为这样的函数在块体中可能有复杂的控制流,并且返回类型对于读取器(有时甚至对于编译器)来说是不明显的

因此,声明compareTo(MyDate)
的正确方法是指定函数的返回类型,如果它包括块体:

operator fun compareTo(other: MyDate): Int {
    when {
        year != other.year -> return year - other.year
        month != other.month -> return month - other.month
        else -> return dayOfMonth - other.dayOfMonth
    }
}
这同时解决了另一个错误,因为需要返回一个
Int

,这只是对的答案的一个补充:您可以将其缩短一点:

operator fun compareTo(other: MyDate) = when {
    year != other.year -> year - other.year
    month != other.month -> month - other.month
    else -> dayOfMonth - other.dayOfMonth
}
这只是对的答案的补充:你可以把它缩短一点:

operator fun compareTo(other: MyDate) = when {
    year != other.year -> year - other.year
    month != other.month -> month - other.month
    else -> dayOfMonth - other.dayOfMonth
}

这有点不对劲。当您编写
funsomefunction(a:Type){}
(没有
=
)时,Kotlin总是会推断返回类型是
单位
。无论他是在
之前
何时
返回
,还是在
中返回,都无关紧要;他只需要在函数定义中添加返回类型(
:Int
)。您不需要将
return
放在
when
之前,因为when表达式的else分支使其详尽无遗。@marstran是的,您当然需要
return
的某个地方。您可以在when表达式的每个分支中使用它,就像他在“第二个case”中所做的那样。这是我的观点。这有点不对劲。当您编写
funsomefunction(a:Type){}
(没有
=
)时,Kotlin总是会推断返回类型是
单位
。无论他是在
之前
何时
返回
,还是在
中返回,都无关紧要;他唯一需要添加的是hi的返回类型(
:Int