Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(KProperty1.lens): Perform check for data class only once. #3315

Merged
merged 2 commits into from
Dec 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import arrow.core.Either
import arrow.core.left
import arrow.core.right
import kotlin.reflect.KClass
import kotlin.reflect.KFunction
import kotlin.reflect.KProperty1
import kotlin.reflect.cast
import kotlin.reflect.full.instanceParameter
import kotlin.reflect.full.memberFunctions
import kotlin.reflect.safeCast
Expand Down Expand Up @@ -34,11 +36,12 @@ public val <S, A> ((S) -> A).ogetter: Getter<S, A>
*
* WARNING: this should only be called on data classes,
* but that is checked only at runtime!
* The check happens when the lens is created.
*/
public val <S, A> KProperty1<S, A>.lens: Lens<S, A>
get() = PLens(
get = this,
set = { s, a -> clone(this, s, a) },
set = reflectiveCopy,
)

/** [Optional] that focuses on a nullable field */
Expand All @@ -54,13 +57,23 @@ public val <S, A> KProperty1<S, List<A>>.every: Every<S, A>
public val <S, K, A> KProperty1<S, Map<K, A>>.values: Every<S, A>
get() = lens compose Every.map()

private fun <S, A> clone(prop: KProperty1<S, A>, value: S, newField: A): S {
private val <S, A> KProperty1<S, A>.reflectiveCopy: (S, A) -> S get() {
// based on https://stackoverflow.com/questions/49511098/call-data-class-copy-via-reflection
val klass = prop.instanceParameter?.type?.classifier as? KClass<*>
val copy = klass?.memberFunctions?.firstOrNull { it.name == "copy" }
if (klass == null || !klass.isData || copy == null) {
val klass = kClass
val copy = klass.copyMethod
val fieldParam = copy.parameters.first { it.name == name }
val instanceParam = copy.instanceParameter!!
return { value, newField -> klass.cast(copy.callBy(mapOf(instanceParam to value, fieldParam to newField))) }
}

@Suppress("UNCHECKED_CAST")
private val <S> KProperty1<S, *>.kClass: KClass<S & Any>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand 100% why the result here is KClass<S & Any>, instead of merely KClass<*>.

get() = instanceParameter?.type?.classifier as? KClass<S & Any> ?: throw IllegalArgumentException("may only be used with instance properties")

private val KClass<*>.copyMethod: KFunction<*> get() {
val copy = memberFunctions.firstOrNull { it.name == "copy" }
if (!isData || copy == null) {
throw IllegalArgumentException("may only be used with data classes")
}
val fieldParam = copy.parameters.first { it.name == prop.name }
return copy.callBy(mapOf(copy.instanceParameter!! to value, fieldParam to newField)) as S
return copy
}