Effect

class Effect<T>
kotlin.Any
   ↳ androidx.compose.Effect

The Effect class is really an opaque class that holds a block of executable code that is meant to be executed positionally in the context of a composition. Since this block is positional, it must also know how to construct its own "key".

NOTE: Effects are equivalent to Composable functions that are able to have a return value, where the unaryPlus operator is the manner in which it is "invoked". Effect will likely go away in favor of unifying with Composable.

Summary

Public methods

Any

This method constructs the key of the effect.

T
resolve(composerContext: Composer<*>, key: Int = sourceLocation())

This is the call that "unwraps" the effect into a value, and participates in positional memoization.

operator V

The unaryPlus operator, in the scope of an effect, is an alias to resolving the effect

Extension functions

From androidx.compose
operator T

IMPORTANT: This global operator is TEMPORARY, and should be removed whenever an answer for contextual composers is reached.

Properties

lateinit Composer<*>

Effects need to interact with the ViewComposer in order to work, however most custom effects will simply compose themselves from the primitives, as opposed to using the ViewComposer directly.

Public methods

constructKey

fun constructKey(key: Int): Any

This method constructs the key of the effect. This is done primarily just as a performance optimization for the common case of dataKey not being defined, which should be the common case

resolve

fun resolve(
    composerContext: Composer<*>,
    key: Int = sourceLocation()
): T

This is the call that "unwraps" the effect into a value, and participates in positional memoization. It is important that this call gets inlined, as it is where the sourceLocation() gets used and does the positional memoization.

The interesting thing here is that we see that an Effect is really little more than a controlled way of creating a group in the slot table.

unaryPlus

operator fun <V> Effect<V>.unaryPlus(): V

The unaryPlus operator, in the scope of an effect, is an alias to resolving the effect

Properties

context

lateinit var context: Composer<*>

Effects need to interact with the ViewComposer in order to work, however most custom effects will simply compose themselves from the primitives, as opposed to using the ViewComposer directly.