What’s passed to `x: Int` if I call `A()`?
# compose
m
What’s passed to
x: Int
if I call
A()
?
0️⃣ 1
@Oleksandr Balan Why
0
? Because it’s the bottom value?
Copy code
class MyClass(/* ... */)

@Composable fun MyComposable(
    myClass: MyClass = MyClass(/* ... */),
) {/* ... */}
What about types without a defined bottom value? What‘s passed to
myClass
if I call
MyComposable()
?
o
For classes it would be
null
, see this example with JvmOverloads here: https://medium.com/@debuggingisfun/kotlin-default-parameter-values-5b0046664c0c
u
Wdym? The specified default would be used
m
@Oleksandr Balan Does that apply to
@Composable
functions too?
o
Yeap, Composables are "just" Kotlin functions, so they have same rules. You can verify it in Android Studio. Tools -> Kotlin -> Show Kotlin Bytecode. For example for these Composable and class:
Copy code
@Composable
fun Foo() {
    Bar()
}

@Composable
fun Bar(a: MyClass = MyClass(42)) {
  // Empty
}

class MyClass(
    val value: Int,
)
Decompiled bytecode looks like this:
Copy code
@Composable
public static final void Foo(@Nullable Composer $composer, int $changed) {
   ...
   Bar((MyClass)null, $composer, 0, 1);
   ...
}

@Composable
public static final void Bar(@Nullable MyClass a, @Nullable Composer $composer, int $changed, int var3) {
   ...
   if ((var3 & 1) != 0) {
       a = new MyClass(42);
       ...
   }
   ...
}
m
@Oleksandr Balan I’ve just tried that and I can’t find the ‘decompile’ button in the latest version of AS. I’m stuck because every tutorial I’ve found online just says to hit the decompile button and doesn’t present any plan Bs. Please help?
Do you either know how to decompile in the latest version of AS, or could you please decompile the following function for me?
Copy code
@Composable
fun LocalDemo(modifier: Modifier = Modifier) {
    val state = remember { mutableIntStateOf(0) }
    Column(modifier = modifier) {
        Text("Tapped ${state.intValue} times.")
        Button(onClick = { state.intValue += 1 }) {
            Text("Tap")
        }
    }

    val tracked = compositionLocalOf { 0 }
    val untracked = staticCompositionLocalOf { 0 }
    CompositionLocalProvider(tracked provides state.intValue) {
        Log.d("LOCALDEMO", "provider where `tracked provides state.intValue`")
    }
    CompositionLocalProvider(untracked provides state.intValue) {
        Log.d("LOCALDEMO", "provider where `untracked provides state.intValue`")
    }
}
o
Huh, strange, I see that button even on Quail 2 (2026.1.2 Canary 2) Sure, here you are, I just replaced
Log.d
with
println
, as I am using CMP project
❤️ 1
m
@Oleksandr Balan I’m also on the latest version, but you see it’s missing in my screenshot, right? (Or is it there and am I just not seeing it?) Did you have to enable something in the settings/preferences or is the IDE supposed to support decompiling right out of the box? Anyway, thank you so much for doing me a favour!
o
Check if you have this plugin enabled? It should ne on by default, but worth checking 🤷
m
@Oleksandr Balan I’m not one to heavily customise my IDE, so I’ve never manually installed/uninstalled or enabled/disabled any plugins. No idea why it’s off, but the decompile button shows up with it on now. Thank you so much for the pointer!
👍 1