Why doesn't Kotlin contain default value for type ...
# announcements
i
Why doesn't Kotlin contain default value for type parameters?
Copy code
class Foo<T = Any>(val t: T = Any())
...
Foo()
g
Because it don’t have this feature %)
🤣 1
It was proposed
there is a feature request about it
r
Would that mean no inference and it’s always the default value unless explicitly specified?
s
Yup, I agree with Gael. I'd rather have it not mess with type inference in my code. I'd rather type the extra chars or use a typealias.
g
I don’t think that it would somehow affect type inference, it make sense for cases when you don’t have any kind inference
r
Yes, sure, but I’m pretty sure it will lead to unexpected result. You would no longer have “type inference failed” errors when you expected it to work and it would then use the default value instead, kinda hiding a bug
s
Sometimes the compiler can and sometimes it cannot infer a type and the compiler seems to get better at it (new inference engine). I'd rather not have surprises....
☝️ 1