New Type Inference in Kotlin 1.3.0-rc-190


#1

Hi,
according to that video https://www.youtube.com/watch?v=MyljSWm0Y_k i would like to try New Type Inference.

But i can not activate it :frowning:
Preconditions:

Android Studio 3.3 Canary 13
Build #AI-182.4505.22.33.5035453, built on September 28, 2018
JRE: 1.8.0_152-release-1248-b01 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14
Gradle 4.10.2 with Kotlin DSL
Kotlin “gradle-plugin” version 1.3.0-rc-190
Kotlin “stdlib-jdk7” version 1.3.0-rc-190

The only available experimental option is “coroutines”.
Please advice! Thank you in advance!


#2

adding following options to the gradle script will fix the issue:

afterEvaluate {
    tasks.withType(KotlinCompile::class)
        .forEach {
            it.kotlinOptions { freeCompilerArgs = listOf("-Xnew-inference") }
        }
}

#3

This has been answered in Slack https://kotlinlang.slack.com/archives/C0922A726/p1539935977000100 but I’ll repeat the answer for others here:

This flag hasn’t landed in the compiler yet. You have to use the old -X flags for now.

Groovy DSL:

compileKotlin {
    kotlinOptions {
        freeCompilerArgs = ["-XXLanguage:+NewInference"]
    }
}

Kotlin DSL:

tasks.withType(KotlinCompile::class)
        .forEach {
            it.kotlinOptions { freeCompilerArgs = listOf("-Xnew-inference") }
        }

Why can't Kotlin infer types here?
#4

Why Kotlin DSL and Grovvy flags are different? doesn’t looks as right snippet, probably only one is correct: Groovy DSL flag is correct, so Kotlin DSL should be:

tasks.withType<KotlinCompile> {
        kotlinOptions {
            freeCompilerArgs += "-XXLanguage:+NewInference"
        }
    }

I also add compiler flags instead of replacing existing ones and use Kotlin DSL API with reified generics to make snippet more Kotlin DSL friendly