Skip to content

Conversation

@rjrjr
Copy link
Collaborator

@rjrjr rjrjr commented Apr 23, 2022

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.

@rjrjr rjrjr marked this pull request as ready for review April 23, 2022 22:35
@rjrjr rjrjr requested review from a team and zach-klippenstein as code owners April 23, 2022 22:35
override fun hashCode(): Int = map.hashCode()

@Suppress("UNCHECKED_CAST")
private fun <T : Any> getOrNull(key: ViewEnvironmentKey<T>): T? = map[key] as? T
Copy link
Contributor

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.

Copy link
Collaborator Author

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.

Comment on lines 37 to 44
other.map.entries.forEach { (key, value) ->
@Suppress("UNCHECKED_CAST")
newMap[key] =
getOrNull(key as ViewEnvironmentKey<Any>)?.let { key.combine(it, value) } ?: value
}
Copy link
Contributor

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

Suggested change
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) }
}

Copy link
Collaborator Author

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
Copy link
Contributor

Choose a reason for hiding this comment

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

nit on naming:

Suggested change
val newPair = getOrNull(newKey)?.let { newKey to newKey.combine(it, newValue) } ?: pair
val newPair = getOrNull(newKey)?.let { oldValue to oldValue.combine(it, newValue) } ?: pair

Copy link
Collaborator Author

@rjrjr rjrjr Apr 25, 2022

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) }
      ?: pair

Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

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

nit or naming

Suggested change
getOrNull(key as ViewEnvironmentKey<Any>)?.let { key.combine(it, value) } ?: value
getOrNull(key as ViewEnvironmentKey<Any>)?.let { existingValue.combine(it, value) } ?: value

Copy link
Collaborator Author

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

@rjrjr
Copy link
Collaborator Author

rjrjr commented Apr 25, 2022

Updated with better naming in the two lambdas calling combine.

`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.
@rjrjr
Copy link
Collaborator Author

rjrjr commented Apr 25, 2022

Update deletes a redundant this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants