-
Notifications
You must be signed in to change notification settings - Fork 112
Introduces ViewEnvironmentKey.combine, makes ViewRegistry use it. #737
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
Conversation
| override fun hashCode(): Int = map.hashCode() | ||
|
|
||
| @Suppress("UNCHECKED_CAST") | ||
| private fun <T : Any> getOrNull(key: ViewEnvironmentKey<T>): T? = map[key] as? T |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth to make it getOrDefault, as all callers are using some sort of fallback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But the main call wants to run a lambda only in the non-null case.
| other.map.entries.forEach { (key, value) -> | ||
| @Suppress("UNCHECKED_CAST") | ||
| newMap[key] = | ||
| getOrNull(key as ViewEnvironmentKey<Any>)?.let { key.combine(it, value) } ?: value | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could try to use Java8 merge method
| other.map.entries.forEach { (key, value) -> | |
| @Suppress("UNCHECKED_CAST") | |
| newMap[key] = | |
| getOrNull(key as ViewEnvironmentKey<Any>)?.let { key.combine(it, value) } ?: value | |
| } | |
| other.map.forEach { key, value -> | |
| newMap.merge(key, value) { valueLeft, valueRight -> key.combine(valueLeft, valueRight) } | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is non-Android, though, and there's a chance of it someday becoming KMP.
| ViewEnvironment(map + pair) | ||
| public operator fun <T : Any> plus(pair: Pair<ViewEnvironmentKey<T>, T>): ViewEnvironment { | ||
| val (newKey, newValue) = pair | ||
| val newPair = getOrNull(newKey)?.let { newKey to newKey.combine(it, newValue) } ?: pair |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit on naming:
| val newPair = getOrNull(newKey)?.let { newKey to newKey.combine(it, newValue) } ?: pair | |
| val newPair = getOrNull(newKey)?.let { oldValue to oldValue.combine(it, newValue) } ?: pair |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you meant this:
val newPair = getOrNull(newKey)
?.let { oldValue -> newKey to newKey.combine(oldValue, newValue) }
?: pairThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes - sorry got lambda confused with the pair vs named argument syntax
| other.map.entries.forEach { (key, value) -> | ||
| @Suppress("UNCHECKED_CAST") | ||
| newMap[key] = | ||
| getOrNull(key as ViewEnvironmentKey<Any>)?.let { key.combine(it, value) } ?: value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit or naming
| getOrNull(key as ViewEnvironmentKey<Any>)?.let { key.combine(it, value) } ?: value | |
| getOrNull(key as ViewEnvironmentKey<Any>)?.let { existingValue.combine(it, value) } ?: value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
newMap[key] = getOrNull(key as ViewEnvironmentKey<Any>)
?.let { oldValue -> key.combine(oldValue, value) }
?: value|
Updated with better naming in the two lambdas calling |
`ViewEnvironmentKey.combine` is called from `ViewEnvironment.plus` when both operands have keys of the same type. `ViewRegistry.Companion` is already a `ViewEnvironmentKey`, and now it implements `combine`, calling `ViewRegistry.merge`. This changes `ViewEnvironment.plus` to merge the values of `ViewRegistry` entries instead of replacing the registry on the left with the one on the right, which is the only thing we've ever actually wanted to do. This allows us to eliminate `ViewEnvironment.merge`. We have never seen a use case for completely stomping the `ViewRegistry` on the left, but have done it by accident a lot. If the need really does arise, we can add a `ViewEnvironment.minus` operator. Also eliminates the `ViewEnvironmentKey()` factory function, which was just silly.
|
Update deletes a redundant |
ViewEnvironmentKey.combineis called fromViewEnvironment.pluswhen both operands have keys of the same type.ViewRegistry.Companionis already aViewEnvironmentKey, and now it implementscombine, callingViewRegistry.merge.This changes
ViewEnvironment.plusto merge the values ofViewRegistryentries instead of replacing the registry on the left with the one on the right, which is the only thing we've ever actually wanted to do. This allows us to eliminateViewEnvironment.merge.We have never seen a use case for completely stomping the
ViewRegistryon the left, but have done it by accident a lot. If the need really does arise, we can add aViewEnvironment.minusoperator.Also eliminates the
ViewEnvironmentKey()factory function, which was just silly.