Kotlin:与lambdas和泛型混淆

时间:2016-05-31 00:33:47

标签: function types kotlin signature generic-variance

请在评论中查看错误消息:

interface Printable {}

class Book(val title: String) :Printable

fun bookPrint(b: Book?):String =  "Title: " + b?.title

class Author(val name: String) :Printable

fun authorPrint(a: Author?):String = "Name: " + a?.name

// Unsupported: [modifier on parameter in function type]
// -------------vv
fun printIt(f: (in Printable?) -> String, a:Printable):String {
    return "Unknown: " + f.invoke(null) +
            "Known: " + f.invoke(a)
}

fun main(args: Array<String>) {
    // Type Mismatch:
    // Required: (Printable?) -> String
    // Found: KFunction1<Book?,String>
    // -------vvvvvvvvv
    printIt(::bookPrint, Book("Storm Front"))
    // -------vvvvvvvvvvv
    printIt(::authorPrint, Author("Jim Butcher"))
}

关键点:

  • bookPrint()authorPrint()都需要采用空书/作者
  • printIt()需要接受其中任何一项功能。

所以,思考&#34;生产者延伸,消费者超级&#34;我认为我的问题是我希望输入参数是协变的,当它被硬编码为逆变时(&#34;在&#34;)。

我有这个想法不起作用:

// Unresolved reference: KFunction1
// --------------vvvvvvvvvv
fun htmlList2(f: KFunction1<Printable?,String>, a:Printable):String {
    return "Unknown: " + f.invoke(null) +
            "Known: " + f.invoke(a)
}

1 个答案:

答案 0 :(得分:2)

我认为这是对的:

interface Printable {}

class Book(val title: String) :Printable

fun bookPrint(b: Book?):String =  "Title: " + b?.title

class Author(val name: String) :Printable

fun authorPrint(a: Author?):String = "Name: " + a?.name

// Add type parameter T, upper-bounded by Printable.  This ties
// the type of the first argument to the type of the second and
// ensures they are both Printable.
fun <T:Printable> printIt(f: (T?) -> String, a:T):String {
    return "Unknown: " + f.invoke(null) +
            "Known: " + f.invoke(a)
}

fun main(args: Array<String>) {
    printIt(::bookPrint, Book("Storm Front"))
    printIt(::authorPrint, Author("Jim Butcher"))
}

感谢您尝试并阅读Generic Constraints: Upper Bounds

上的文档

仍有兴趣,如果有更好的方式...