Status Update
Comments
ad...@google.com <ad...@google.com>
as...@google.com <as...@google.com>
as...@google.com <as...@google.com> #2
After some investigation, is seems like we restrict defaults for abstract
functions, but don't do so for open
ones. Long-term, we need to support this code shape, short-term we likely want to restrict these.
ap...@google.com <ap...@google.com> #3
Project: platform/frameworks/support
Branch: androidx-main
commit 349bcc021df2ec23df85344bd67468bf1140579c
Author: Andrei Shikov <ashikov@google.com>
Date: Tue Jan 02 23:57:19 2024
Restrict default parameters on open composable functions
Compose doesn't support substituting default parameters from overridden functions and currently restricts abstract functions from using default params. This change expands this restriction to open functions, which were previously ignored.
Relnote: """Add a compilation error for default parameters in open functions (previously only it was restricted only for abstract functions). Compose does not support substitution of default parameters from overridden functions at the moment, and overriding a composable function with default parameters was failing at runtime. Use `@Suppress("ABSTRACT_COMPOSABLE_DEFAULT_PARAMETER_VALUE") to suppress, but note that overriding such function without providing a default value for parameter will result in a crash at runtime. """
Bug: 317490247
Change-Id: Iaa00663f630e51900341b9603b368dfc54662582
M compose/compiler/compiler-hosted/integration-tests/src/jvmTest/kotlin/androidx/compose/compiler/plugins/kotlin/analysis/ComposableDeclarationCheckerTests.kt
M compose/compiler/compiler-hosted/src/main/java/androidx/compose/compiler/plugins/kotlin/k1/ComposableDeclarationChecker.kt
M compose/compiler/compiler-hosted/src/main/java/androidx/compose/compiler/plugins/kotlin/k1/ComposeErrorMessages.kt
M compose/compiler/compiler-hosted/src/main/java/androidx/compose/compiler/plugins/kotlin/k2/ComposableFunctionChecker.kt
M compose/compiler/compiler-hosted/src/main/java/androidx/compose/compiler/plugins/kotlin/k2/ComposeErrorMessages.kt
M compose/material/material/api/current.txt
M compose/material/material/api/restricted_current.txt
M compose/material/material/src/androidMain/kotlin/androidx/compose/material/ExposedDropdownMenu.android.kt
M compose/material3/material3/api/1.2.0-beta02.txt
M compose/material3/material3/api/current.txt
M compose/material3/material3/api/restricted_1.2.0-beta02.txt
M compose/material3/material3/api/restricted_current.txt
M compose/material3/material3/src/androidMain/kotlin/androidx/compose/material3/ExposedDropdownMenu.android.kt
https://android-review.googlesource.com/2894244
Branch: androidx-main
commit 349bcc021df2ec23df85344bd67468bf1140579c
Author: Andrei Shikov <ashikov@google.com>
Date: Tue Jan 02 23:57:19 2024
Restrict default parameters on open composable functions
Compose doesn't support substituting default parameters from overridden functions and currently restricts abstract functions from using default params. This change expands this restriction to open functions, which were previously ignored.
Relnote: """Add a compilation error for default parameters in open functions (previously only it was restricted only for abstract functions). Compose does not support substitution of default parameters from overridden functions at the moment, and overriding a composable function with default parameters was failing at runtime. Use `@Suppress("ABSTRACT_COMPOSABLE_DEFAULT_PARAMETER_VALUE") to suppress, but note that overriding such function without providing a default value for parameter will result in a crash at runtime. """
Bug: 317490247
Change-Id: Iaa00663f630e51900341b9603b368dfc54662582
M compose/compiler/compiler-hosted/integration-tests/src/jvmTest/kotlin/androidx/compose/compiler/plugins/kotlin/analysis/ComposableDeclarationCheckerTests.kt
M compose/compiler/compiler-hosted/src/main/java/androidx/compose/compiler/plugins/kotlin/k1/ComposableDeclarationChecker.kt
M compose/compiler/compiler-hosted/src/main/java/androidx/compose/compiler/plugins/kotlin/k1/ComposeErrorMessages.kt
M compose/compiler/compiler-hosted/src/main/java/androidx/compose/compiler/plugins/kotlin/k2/ComposableFunctionChecker.kt
M compose/compiler/compiler-hosted/src/main/java/androidx/compose/compiler/plugins/kotlin/k2/ComposeErrorMessages.kt
M compose/material/material/api/current.txt
M compose/material/material/api/restricted_current.txt
M compose/material/material/src/androidMain/kotlin/androidx/compose/material/ExposedDropdownMenu.android.kt
M compose/material3/material3/api/1.2.0-beta02.txt
M compose/material3/material3/api/current.txt
M compose/material3/material3/api/restricted_1.2.0-beta02.txt
M compose/material3/material3/api/restricted_current.txt
M compose/material3/material3/src/androidMain/kotlin/androidx/compose/material3/ExposedDropdownMenu.android.kt
Ch...@esolutions.de <Ch...@esolutions.de> #4
I had the same issue, when I wanted to provide a unified library API for different OEMs using interfaces/abstract classes with default parameters.
Note that restricting defaults on abstract/open functions is not consistent with the way kotlin handles default parameters override (which could be suprising for other users):
https://kotlinlang.org/docs/functions.html#default-arguments
But at least having a compile time error would be better
Note that restricting defaults on abstract/open functions is not consistent with the way kotlin handles default parameters override (which could be suprising for other users):
But at least having a compile time error would be better
Description
Setting default parameters in a Composable function in an Interface, and then overriding it will crash at runtime with a null value for those parameters. According to Ben Trengrove on Kotlin slack "I should add as well, default values in composable interface functions are currently not supported. The compiler should be picking that up at compile time though, not runtime."
Jetpack Compose version: Compiler 1.5.7 androidx.compose:compose-bom:2023.10.01
Jetpack Compose component(s) used: Compiler/Runtime Android Studio Build: Android Studio Hedgehog | 2023.1.1
Kotlin version: 1.9.21
Steps to Reproduce or Code Sample to Reproduce:
Stack trace (if applicable):