【发布时间】:2019-02-17 19:40:55
【问题描述】:
我是 Kotlin 的新手,我正在尝试了解类型别名和函数。
我有以下示例:
interface EmptyInterface
typealias GenericCase<T> = T.(EmptyInterface) -> T
val myFunctionVariable: GenericCase<String> = {
_ -> "Hello world!"
}
到目前为止,我的理解是,我扩展了 T 定义的函数,该函数接受 EmptyInterface 作为参数并返回 T。
所以myFunctionVariable 是一个应该通过EmptyInterface 调用的函数
但是下面的代码不能编译
class a: EmptyInterface
println("${myFunctionVariable(a())}")
我需要传递一个String 作为第一个参数:
class a: EmptyInterface
println("${myFunctionVariable("",a())}")
为什么需要字符串作为第一个参数? T.(EmptyInterface) -> T 在这种情况下是 String.(EmptyInterface) -> String 只有 1 个参数。
有人可以解释一下吗?
【问题讨论】:
标签: kotlin kotlin-extension kotlin-function-type