sharing
fun <ID : Any, Initial, Return> Aggregate<ID>.sharing(initial: Initial, transform: YieldingContext<Initial, Return>.(Field<ID, Initial>) -> YieldingResult<Initial, Return>): Return
sharing captures the space-time nature of field computation through observation of neighbors' values, starting from an initial value, it reduces to a single local value given a transform function and updating and sharing to neighbors of a local variable.
val result = sharing(0) {
val maxValue = it.maxBy { v -> v.value }.value
maxValue.yielding { "Something different" }
}
result // result: kotlin.StringContent copied to clipboard
In the example above, the function sharing will return the string initialized as in yielding.
Invalid use:
Do not write code after calling the sending or returning values, they must be the last statement of the body (the lambda expression).
share(0) {
val maxValue = it.maxBy { v -> v.value }.value
maxValue.yielding { "Don't do this" }
maxValue
}Content copied to clipboard