So I may be missing something super basic, but I c...
# announcements
l
So I may be missing something super basic, but I cannot find an easy way to help the compiler determine the return type for a function with multiple type parameters, one of which is the return type, without specifying all of the type arguments. This is annoying when the other type arguments can be inferred easily. For example:
Copy code
fun <A, B, R> Pair<A, B>.foo(block: (A, B) -> R): R = block(first, second)

Pair(1, 2).foo { a, b -> println("a: $a, b: $b") }  // Compiler complains that type inference failed for R
EDIT: This example actually works for my in the REPL, although I have a more complex example for which this doesn't work (the
val _: Unit
trick below fixes the problem though), a more basic example is
select
which requires the explicit
<Unit>
type if used like a statement. That case is not problematic though since there's a single type argument.