Constructor with default values

Is there a solution to provide sensible default values for constructor parameters automatically?

For example in case of the following class:

@WithDefaults
class A(p1 : Int, p2 : String, p3 : String?, p4: List<Long>, p5 : SomeClass) {
}

I would like to have an empty constructor added automatically:

@WithDefaults
class A(p1 : Int, p2 : String, p3 : String?, p4 : List<Long>) {
    constructor() : this(0, "", null, emptyList(), SomeClass())
}

The whole point would be not to write out the default values.

I think is not equivalent to the no-args compiler plugin as:

  1. it have to actually set values, paying attention to nullability and making class instances
  2. should be multiplatform
  3. should be accessible from normal Kotlin code

I’ll probably write this if there is not one out there already, hence asking.

Your proposal means indirectly that default constructor should be un-editable, IMHO. It is irretrievable. Otherwise it will be problematic by deriving and using of those clsses with default constructors by call chaining, wenn by one default constructor should use other object (evtl. default constructor).

If a plugin generates it - like kotlinx.serialization does for the serializer - it will be un-editable. It won’t even show up in the source code, but you would be able to use it.

Of course, if you write one manually, the plugins should realize that there is one already and skip the automatic generation.