Suppose I have a class `Foo(val bar: Int = 2)`. No...
# announcements
k
Suppose I have a class
Foo(val bar: Int = 2)
. Now I have a factory method that delegates to the constructor and it should be possible to either pass a value for
bar
or just use the default value. I can write
Copy code
fun createFoo(bar: Int? = null) = Foo(bar ?: 2)
but that duplicates the default value which is pointless. I could also write
Copy code
fun createFoo(bar: Int? = null) = if (bar != null) Foo(bar) else Foo()
but that doesn't scale if I have more than one default parameter. I could also just omit the default value in the constructor and put it in the factory method but it seems to be the wrong place (or is it?). What's your opinion?