Is there any news on whether we'll get default arg...
# language-proposals
m
Is there any news on whether we'll get default arguments for generic parameters? 🙂 à la
class Something<Context = DefaultContext>
->
val something: Something
🤔 1
3
d
Would this work exactly like function parameters, with the exception that supertype declarations are not necessary?
Copy code
class Something<Required, Extra = Unit>
val a: Something<String, Extra = Int>
Copy code
class Something<Elem, Coll : Collection<Elem> = List<Elem>>

val a: Something<String, Set<String>>
m
I would love too see that 🙂 And +1 for the named generic arguments
k
Would love to see named + default type parameters just like function parameters. I wonder if there's a technical reason this can't be implemented?
c
I’m struggling to find a downside, probably more a matter of priority and resources I’d guess.
i
Note that after introducing named generic arguments, renaming generic parameters would be no longer a source-compatible change.
d
There is the option of having only indexed type parameters with default values, and not using parameters by name. That's not what the poster asked for in the end. I also struggle to see how useful it would really be. We generally shouldn't encourage high numbers of generic type arguments right?
m
Yeah, generic types shouldn't become too complicated. In one of my cases where default parameters would really help I have a single generic parameter called
context
so that you can pass additional domain-specific data and functionality around in a type-safe way. But if you don't need that then you constantly have to specify something like
<SomeEmptyContextIAmNotUsingAnyway>
. So just defaulting that away would be awesome and remove a lot of visual clutter 🙂
It's fine to just haved it for indexed parameters for now as with few generic parameters you can easily reorder them and put the most-likely default ones to the end. Having generic parameters on par with function parameters (named etc.) would be great long-term but can be considered separately.
t
have you tried
typealias SomethingDefault = Something<DefualtContext>
->
val something: SomethingDefault
?
m
That way I'd have to introduce dozens of aliases and the library user would still have to write something special all the time for something they don't care about.
t
Java users wrote constructor overloads for decades 😃
😄 1
d
kek