简体   繁体   English

为什么不能将“kotlin.Result”用作返回类型?

[英]Why can't 'kotlin.Result' be used as a return type?

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我创建了一个方法,返回的是MyClass<R>类中的Result<R> ,但错误信息是: 'kotlin.Result' cannot be used as a return type

I've also looked into the Result source code for some hints;我还查看了 Result 源代码以获得一些提示; why is this so?为什么会这样?

Test code (using v. 1.3-RC). 测试代码(使用 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)   
}

From the Kotlin KEEP :来自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.这些限制背后的基本原理是,Kotlin 的未来版本可能会扩展和/或更改返回 Result 类型的函数的语义,而 null 安全运算符在用于 Result 类型的值时可能会更改其语义。 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.为了避免在 Kotin 的未来版本中破坏现有代码并为这些更改敞开大门,相应的使用现在会产生错误。 Exceptions to this rule are made for carefully-reviewed declarations in the standard library that are part of the Result type API itself.此规则的例外是标准库中经过仔细审查的声明,这些声明是 Result 类型 API 本身的一部分。

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 .注意:如果您只想试验Result类型,您可以通过提供 Kotlin 编译器参数-Xallow-result-return-type来绕过此限制。

When using Gradle on Java or Android project: Define the compiler argument on Kotlin compilation task.在 Java 或 Android 项目上使用 Gradle 时:在 Kotlin 编译任务上定义编译器参数。 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.在多平台项目上使用 Gradle 时:为每个目标编译定义编译器参数。 It applies both for production code and tests.它适用于生产代码和测试。

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

If you using android this solution for gradle如果你使用android这个gradle这个解决方案

If using maven:如果使用 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:如果使用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/资料来源: http : //rustyrazorblade.com/post/2018/2018-12-06-kotlin-result/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM