gpt4 book ai didi

android - Title : Type inference failed. Expected type mismatch : inferred type is @Composable, 为什么我不能直接记住 mutableStateOf Composable Function?

转载 作者:行者123 更新时间:2023-12-05 02:26:14 31 4
gpt4 key购买 nike

我可以拥有

val composeFunction = remember { mutableStateOf ({}) }

我可以拥有

val composeFF = @Composable { Text("ABC") }
val composeFunction = remember { mutableStateOf (composeFF) }

为什么我不能

val composeFunction = remember { mutableStateOf (@Composable { Text("ABC") }) }

错误状态

Internal Error occurred while analyzing this expression 
(Please use the "

" icon in the bottom-right corner to report this error):

jar:file:/Applications/Android%20Studio.app/Contents/lib/platform-impl.jar!/general/error.svg
Type inference failed. Expected type mismatch: inferred type is @Composable () -> Unit but () -> Unit was expected

最佳答案

您是否尝试过指定类型?

val composeFunction = remember { mutableStateOf<@Composable () -> Unit> (@Composable { Text("ABC") }) }

看起来编译器无法将普通函数推断为带有 @Composable 注释的东西

更新:

事实证明,@Composable 注解实际上修改了函数类型,类似于 suspend 修饰符的作用。

基于此arcticle ,

An important thing to note is that Compose is not an annotationprocessor. Compose works with the aid of a Kotlin compiler plugin inthe type checking and code generation phases of Kotlin: there is noannotation processor needed in order to use compose. This annotationmore closely resembles a language keyword. A good analogy is Kotlin’ssuspend keyword.

此外,

The important point here is that when you annotate a function typewith @Composable you’re changing its type: the same function typewithout the annotation is not compatible with the annotated type.Also, suspend functions require a calling context, meaning that youcan only call suspend functions inside of another suspend function.

关于android - Title : Type inference failed. Expected type mismatch : inferred type is @Composable, 为什么我不能直接记住 mutableStateOf Composable Function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73996176/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com