admin管理员组文章数量:1277896
I have already updated my Android project to use Kotlin 2.1.0.
I added this config to my build.gradle.kts
and then synced project.
kotlin {
compilerOptions {
freeCompilerArgs.add("-Xwhen-guards")
}
}
But when I use "when guards" like this
fun processNotification(notification: Notification) {
when (notification) {
is Notification.Email if notification.isHighPriority -> println("Send high-priority email")
is Notification.Email -> println("Send regular email")
is Notification.SMS if notification.isInternational -> println("Send international SMS")
is Notification.SMS -> println("Send local SMS")
else -> println("Unknown notification type")
}
}
Android Studio still shows this error
The feature "when guards" is experimental and should be enabled explicitly
Do I miss something important?
P/s: I'm using Android Studio Ladybug Feature Drop | 2024.2.2 Patch 1
I have already updated my Android project to use Kotlin 2.1.0.
I added this config to my build.gradle.kts
and then synced project.
kotlin {
compilerOptions {
freeCompilerArgs.add("-Xwhen-guards")
}
}
But when I use "when guards" like this
fun processNotification(notification: Notification) {
when (notification) {
is Notification.Email if notification.isHighPriority -> println("Send high-priority email")
is Notification.Email -> println("Send regular email")
is Notification.SMS if notification.isInternational -> println("Send international SMS")
is Notification.SMS -> println("Send local SMS")
else -> println("Unknown notification type")
}
}
Android Studio still shows this error
The feature "when guards" is experimental and should be enabled explicitly
Do I miss something important?
P/s: I'm using Android Studio Ladybug Feature Drop | 2024.2.2 Patch 1
Share Improve this question asked Feb 24 at 11:18 Qk LahpitaQk Lahpita 4672 gold badges12 silver badges19 bronze badges1 Answer
Reset to default 1This experimental feature is only available with the new K2 compiler introduced in Kotlin 2.0.0. Since you use Kotlin 2.1.0 your code should compile just fine. Just try building your app.
However, your Android Studio version still uses the old Compiler internally for syntax highlighting, code completion, code inspection and so on. That compiler does not know of the experimental feature and issues an error. This does not affect actually building and running your app, though. It is just a display issue.
Your Android Studio version actually does support the new K2 compiler already, but that feature is still in beta and deactivated by default. If you want to try it out you can activate it in the settings:
File | Settings | Languages & Frameworks | Kotlin | Enable K2 mode
After a restart Android Studio shouldn't display an error anymore.
版权声明:本文标题:android - The feature "when guards" is experimental and should be enabled explicitly - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741274189a2369632.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论