124

I've created a method, and the return is Result<R> in a class of MyClass<R>, but the error message is: 'kotlin.Result' cannot be used as a return type

I've also looked into the Result source code for some hints; why is this so?

Test code (using v. 1.3-RC).

class MyClass<R>(val r: R) {
    fun f(): Result<R> { // error here
        return Result.success(r)
    }
}

fun main(args: Array<String>) {
    val s = Result.success(1)
    val m = MyClass(s)   
}
3
  • Show us your code that causes the error. Where does kotlin.Result come from? I cannot find that class in the standard library.
    – Jesper
    Oct 3, 2018 at 16:42
  • Try kotlin 1.3 or newer Oct 3, 2018 at 16:57
  • 3
    The limitations on Kotlin Result type are lifted in version 1.5, you can freely use the type as a return type.
    – HumbleBee
    Jul 19, 2021 at 11:03

4 Answers 4

80

From the Kotlin KEEP:

The rationale behind these limitations is that future versions of Kotlin may expand and/or change semantics of functions that return Result type and null-safety operators may change their semantics when used on values of Result type. In order to avoid breaking existing code in the future releases of Kotin and leave door open for those changes, the corresponding uses produce an error now. Exceptions to this rule are made for carefully-reviewed declarations in the standard library that are part of the Result type API itself.

Note: if you just want to experiment with the Result type you can bypass this limitation by supplying a Kotlin compiler argument -Xallow-result-return-type.

When using Gradle on Java or Android project: Define the compiler argument on Kotlin compilation task. It applies both for production code and tests.

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = freeCompilerArgs + "-Xallow-result-return-type"
    }
}

When using Gradle on Multiplatform project: Define the compiler argument for each target compilation. It applies both for production code and tests.

kotlin {
    targets.all {
        compilations.all {
            kotlinOptions {
                freeCompilerArgs = freeCompilerArgs + "-Xallow-result-return-type"
            }
        }
    }
}
4
46
android {
    kotlinOptions {
        freeCompilerArgs = ["-Xallow-result-return-type"]
    }
}

If you using android this solution for gradle

1
  • 1
    Kotlin DSL: freeCompilerArgs = listOf("-Xallow-result-return-type")
    – Micha
    Jan 11, 2021 at 21:43
9

If using maven:

<plugin>
    <artifactId>kotlin-maven-plugin</artifactId>
    <configuration>
        <jvmTarget>1.8</jvmTarget>
        <args>
            <arg>-Xallow-result-return-type</arg>
        </args>
    </configuration>
    <groupId>org.jetbrains.kotlin</groupId>
    <version>${kotlin.version}</version>

If using gradle:

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
    kotlinOptions.freeCompilerArgs = ["-Xallow-result-return-type"]


}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
    kotlinOptions.freeCompilerArgs = ["-Xallow-result-return-type"]
}

Source: http://rustyrazorblade.com/post/2018/2018-12-06-kotlin-result/

8

Update the kotlin version to 1.5 or above. See:

Screenshot of proposal https://github.com/Kotlin/KEEP/blob/master/proposals/stdlib/result.md#limitations-legacy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.