Is there an object overload for data class copy function?

I know you can copy an instance of a data class using the copy function, and that it makes named parameters matching the fields that should be changed.

I’m wondering though if there is any overload that takes an object.

My use case:

kotlin val someObject = DataKlazz( foo = "hi", bar = "bye", baz = "hello", fieldOne = "one", fieldTwo = "two", fieldThree = "three", ..... // other fields ) val copiedObject = if (someBooleanExpression) { someObject.copy(foo = "yello", bar = "green") } else { someObject.copy( foo = "yello", bar = "green", fieldOne = "hello", fieldThree = "bye" ) }

I’d like to simplify the named parameters are conditional, so then the duplicate named parameters for the branched copy calls are consolidated. So something like this (using the someObject from above):

kotlin val changesObject = new object { val foo = "yello", val bar = "green" } val finalChanges = if (someBooleanExpression) { new object { *changesObject , // spread the fields from changesObject val fieldOne = "hello", val fieldThree = "bye" } } else { changesObject } someObject.copy(finalChanges)

I know that there you can’t “spread” objects in Kotlin the way you can in JavaScript. I’m wondering though if there’s a way to achieve the succinctness I’m looking for i.e. an overload for copy I’m not aware of.

submitted by /u/icantsI33p
[link] [comments]