Home > Software engineering >  What is this "provides" syntax in this kotlin jetpack compose code sample?
What is this "provides" syntax in this kotlin jetpack compose code sample?

Time:12-21

What is the "provides" syntax in this code sample and what does it do?

LocalContentAlpha provides ContentAlpha.medium

It doesn't seem to be a standard kotlin keyword and I haven't had much luck googling for queries like "kotlin provides keyword" or "jetpack compose provides".

This shows up on the Jetpack Compose codelab, full snippet below.

@Composable
fun PhotographerCard() {
    Column {
        Text("Alfred Sisley", fontWeight = FontWeight.Bold)
        // LocalContentAlpha is defining opacity level of its children
        CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.medium) {
            Text("3 minutes ago", style = MaterialTheme.typography.body2)
        }
    }
}

@Preview
@Composable
fun PhotographerCardPreview() {
    LayoutsCodelabTheme {
        PhotographerCard()
    }
}

CodePudding user response:

This is an example of an infix function:

Functions marked with the infix keyword can also be called using the infix notation (omitting the dot and the parentheses for the call).

As seen by the existence of the infix keyword on the method's documentation.

So the method could be called normally as LocalContentAlpha.provides(ContentAlpha.medium), but the infix notation allows for those extra syntax characters to be dropped.

  • Related