Type inference in lambda default argument

This post seems to be related: Nulls and generics - #7 by ilya.gorbunov

An idea was proposed there that default parameter value could add some additional constraints to the parameter type, but these constraints would be only honored by the compiler if the actual value for that parameter is missing.

For example in this function declaration:
fun <T> foo(x: Int = 1, conv: (Int) -> T = { it })
the default value of conv could add a constraint T >: Int (T should be supertype of Int), but that constraint would be ignored if you specify the value of conv when calling the function.

And if you omit the argument, the constraint could help to report an error about the invalid actual type parameter:

foo<String>(1) // ERROR: String is not supertype of Int
foo(1, { it.toString() }) // returns "1"
foo<Int>(1) // returns 1
foo(1) // returns 1
1 Like