Francesc
04/27/2025, 9:21 PMcontext parameters
, do all intermediate functions need to declare them, even if only the last function in the stack accessed them? I have a sample in thread where that seems to be the case as it won't compile otherwise, but I'd like to confirmFrancesc
04/27/2025, 9:21 PM// won't compile if commening out this first line
context(sharedTransitionScope: SharedTransitionScope, animatedVisibilityScope: AnimatedVisibilityScope)
@Composable
fun DetailsBox(
color: Int,
modifier: Modifier = Modifier
) {
Box(
modifier = modifier,
) {
InnerBox(
color = color,
modifier = Modifier.fillMaxWidth(.75f)
)
}
}
context(sharedTransitionScope: SharedTransitionScope, animatedVisibilityScope: AnimatedVisibilityScope)
@Composable
fun InnerBox(
color: Int,
modifier: Modifier = Modifier
) {
with(sharedTransitionScope) {
Box(
modifier = modifier
.sharedElement(
sharedContentState = rememberSharedContentState(key = "color-${color}"),
animatedVisibilityScope = animatedVisibilityScope,
)
.fillMaxWidth(.75f)
.aspectRatio(1f)
.background(color = Color(color)),
)
}
}
Youssef Shoaib [MOD]
04/27/2025, 9:23 PMFrancesc
04/27/2025, 9:23 PMJoffrey
04/27/2025, 9:28 PM_
Edgar Avuzi
05/01/2025, 5:26 PMcontext
in the language is to improve how the code looks?
Letting pass parameters without listing them in the function parameters list - just another mean of doing the same thing?
Letting context
parameters implicitly pass down the call stack when calling another function?
Just yet another stylistic thing? I suppose it is for much more, but as I used this feature, I do not see more.
Somewhere I read
But this is achievable with function parametersparameters are about capability-oriented design — letting you say what this function needs to run in a type-safe, composable, and declarative way, without bloating your parameter list or leaking implementation details.context
Youssef Shoaib [MOD]
05/01/2025, 5:27 PMYoussef Shoaib [MOD]
05/01/2025, 6:23 PMinterface Plus<T> {
operator fun T.plus(other: T)
}
// Bridge function
context(p: Plus<T>)
operator fun <T> T.plus(other: T) = with(p) { plus(other) }
interface HasZero<T> {
val zero: T
}
interface Inversible<T> {
operator fun T.unaryMinus()
}
context(hz: HasZero<T>)
val zero: T get() = hz.zero
context(_: Plus<T>, _: HasZero<T>, _: Inversible<T>)
operator fun <T> T.times(count: Int) = if (count < 0) -(this * (-count)) else {
val res = zero
repeat(count) { res += this }
return res
}
Which is amazing!Michael Krussel
05/01/2025, 6:56 PM