C# style object initialiser


#1

Java style double brace initialization is bulky and I would avoid this as it has some unexpected side-effects because this syntax actually creates an anonymous inner class. I expected C# pattern object initializers in Kotlin as it promises everything concise.
Why is there no such thing as C# pattern object initializers in Kotlin like for instance
Employee emp_001= new Employee
{
Age = 25,
Name = “Javed Khan”
};

This would have been a very powerful and handy feature.


#2

There’s the apply method:

val emp_001 = Employee().apply {
    age = 25
    name = "Javed Khan"
}

#3

I never felt the need to initialize an object with apply, since constructors (or factory methods) with named parameters and default values always worked perfectly fine for me.

val emp_001 = Employee(age = 25, name = "Javed Khan")

#4

There are a sea of options in Kotlin to initialize variables, using apply , using run, using let and also for disambuation. However, all the above options require that all properties to initialize are vars. The best way is a constructor.