moko-kswift icon indicating copy to clipboard operation
moko-kswift copied to clipboard

Provide Swift friendly alternative to data class copy methods

Open dalewking opened this issue 3 years ago • 0 comments

Similar to #8, but requires a different solution There is no good Swift way to call a copy methods on a Kotlin data class because the default parameter values are not provided, but it isn't possible to actually have parameters default to values based on the object so it is not possible to write an exact replica of the city function. It is.t important that what is generated be the same syntactically. It is more important to get the same functionality, e.g to make a copy of the data class changing only part of the object without having to specify every property.

Here is a SO post that talks about how to write such a method in Dart, which I know is not Swift, but the same concepts and limitations apply: https://stackoverflow.com/questions/68009392/dart-custom-copywith-method-with-nullable-properties

The best alternative is probably the one that takes nullable parameters that default to nil and if it is nil the property is unchanged. The wrinkle is for properties that were nullable, in which case you add a boolean parameter for each such property that says whether to treat null as null or unchanged.

dalewking avatar Sep 14 '22 08:09 dalewking