greetings all, I’m wondering about initializing objects via a constructor vs an extension function.
Example via constructor:
data class CarDTO(
val make: String,
val model: String,
val year: Int
)
data class Car(
val make: String,
val model: String,
val year: Int
) {
constructor(carDTO: CarDTO) : this(
make = carDTO.make,
model = carDTO.model,
year = carDTO.year
)
}
Example via extension function:
data class CarDTO(
val make: String,
val model: String,
val year: Int
) {
fun toCar(): Car {
return Car(
make = this.make,
model = this.model,
year = this.year
)
}
}
data class Car(
val make: String,
val model: String,
val year: Int
)
Is there an advantage to using one of these over the other? I’ve been doing some research, but can’t find any arguments either way.