1

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() and authorPrint() both need to take a null book/author
  • printIt() 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)
}
GlenPeterson
  • 4,866
  • 5
  • 41
  • 49

1 Answers1

3

I think this is right:

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"))
}

Thanks to experimenting and reading the documentation on Generic Constraints: Upper Bounds

Still interested if there is a better way...

GlenPeterson
  • 4,866
  • 5
  • 41
  • 49