Please see error messages in comments:
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"))
}
Key points:
bookPrint()
andauthorPrint()
both need to take a null book/authorprintIt()
needs to take either of those functions.
So, thinking "producer-extends, consumer-super" I think that my problem is that I want an input parameter to be covariant, when it's hard-coded to be contravariant ("in").
I had this idea that didn't work:
// Unresolved reference: KFunction1
// --------------vvvvvvvvvv
fun htmlList2(f: KFunction1<Printable?,String>, a:Printable):String {
return "Unknown: " + f.invoke(null) +
"Known: " + f.invoke(a)
}