sharing
fun <Initial, Return> AggregateContext.sharing(initial: Initial, transform: SharingContext<Initial, Return>.(Field<Initial>) -> SharingResult<Initial, Return>): Return
sharing captures the space-time nature of field computation through observation of neighbours' values, starting from an initial value, it reduces to a single local value given a transform function and updating and sharing to neighbours 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 share wil return the string initialised as in sendButReturn.
Invalid use:
Do not write code after calling the sending or returning values, they must be written at last inside the lambda.
share(0) {
val maxValue = it.maxBy { v -> v.value }.value
maxValue.yielding { "Don't do this" }
maxValue
}Content copied to clipboard