kopykat-kt / kopykat

Little utilities for more pleasant immutable data in Kotlin
Other
283 stars 16 forks source link

Support value classes #3

Closed pablisco closed 2 years ago

pablisco commented 2 years ago

We can support value class copy constructors in two ways:

Copy constructor:

We can add a very simple copy constructor:

value class Temperature(val value: Int)

fun Temperature.copy(block: (Int) -> Int): Temperature

// usage
val newTemp = temp.copy { it + 1 }

Nested copy

When making a copy of a type that we have generated copy/transform functions, we can avoid the user having to reference the single value in the type. 

This may need more thinking and potentially make it opt-in (at least initially)