Peter
05/12/2021, 8:01 PM// Java approach works of course
fun <T> test1(a: T) = a
fun test1() = test1("World")
// Not allowed, gives an error
fun <T> test2(a: T = "World") = a
ephemient
05/12/2021, 8:25 PMT != String
?ephemient
05/12/2021, 8:26 PMfun <T : Any> test3(a: T? = null) = a
but you have to provide a value that is actually validRuckus
05/12/2021, 8:27 PMusing the Java pattern that Kotlin was suppose to fixI don't understand what you mean by this. Could you elaborate? Also, I'm not sure what you expect here. There are all sorts of edge cases here. for example, what about
fun <T> test(a: T = "Hello", b: T = 7): T { ... }
Peter
05/12/2021, 9:21 PMephemient
05/12/2021, 9:25 PMtest2<Int>()
ephemient
05/12/2021, 9:25 PMPeter
05/12/2021, 9:25 PMPeter
05/12/2021, 9:29 PMRuckus
05/12/2021, 9:29 PMfun <T> test(a: T = 9.0, b: T = a / 2.0): T { ... }
test("Help")
Ruckus
05/12/2021, 9:34 PMSo not sure why test2<Int>() is expected to be legal.Because
Int
is within the type bounds of the function. Default values are just that, defaults. They shouldn't change the specification of the function when used.Ruckus
05/12/2021, 9:37 PMPeter
05/12/2021, 9:49 PMRuckus
05/12/2021, 9:54 PM