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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

This 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.

本文标签: androidThe feature quotwhen guardsquot is experimental and should be enabled explicitlyStack Overflow