Kotlin函数语法
我做Kotlin Koans测试是为了熟悉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
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
)