Define default value for a type


#1

Maybe could be useful can define default value for a types for delete boilerplate

Example:


class Person {
  var name: String = ""
  var lastName: String = ""
  var address: String = ""
}
String.default  =  ""

class Person {
  var name := String
  var lastName := String
  var address := String
}

When you use ** := ** you must import the default value from a file


#2

+1 I’m also struggling with this; especially how to deal with generics if there is no defaults in kotlin (is there?). If there is generic property, does it need to be forced into a nullable, to be able to initialize it? And everything needs dummy =null? looking for something like default(T) in c#


#3

“Default” values as you suggest are often poor design. The philosophy is that the object is always in a valid state except while executing the constructor (and finalizer). Basically all values are provided by the constructor based on its parameters. The primary constructor syntax with variables is a concrete push towards this concept. The only way to initialize generics is when the generic value is assigned through the constructor as it disappears at runtime and the class cannot interrogate its generic parameters.