Kotlin newby here. I have a set of functions that accept and parse different inputs (plain text, json, xml) but that have the same output (and instance of Event). The code looks as follows (complete version at https://pastebin.com/UNJFGZsm):
data class Event(val id: Int) val stringToEvent: (String) -> Event = { s -> Event(s.toInt()) } val dummyToEvent: (Document) -> Event = { _ -> Event(1) } val jsonToEvent: (JsonNode) -> Event = { j -> Event(j.get("id").asInt()) } fun elementGen(opt: String): Any { // return a String, or a JsonNode, or a Document // ... } fun main(args : Array) { val parser = when (args[0]) { "string" -> stringToEvent // it builds if I remove this line "json" -> jsonToEvent "xml" -> dummyToEvent else -> throw RuntimeException("Option not supported") } print(parser(elementGen(args[0]))) }
When I try to build I get the error that follows:
(44, 11): Out-projected type 'Function1<*, Event>' prohibits the use of 'public abstract operator fun invoke(p1: P1): R defined in kotlin.Function1
However, the code seems to build and work correctly if I don't use the stringToEvent
function.
Why is that? Why does the problem only seem to effect the (String) -> Event
type function?