Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何避免kotlin中繁琐的支架_Kotlin - Fatal编程技术网

如何避免kotlin中繁琐的支架

如何避免kotlin中繁琐的支架,kotlin,Kotlin,我必须处理以下java代码 abstract class ScriptResult { void ifSuccess(Consumer<Object> block) { } void ifError(Consumer<Throwable> block) { } static class Success extends ScriptResult { private final Object returnValue;

我必须处理以下java代码

abstract class ScriptResult {
    void ifSuccess(Consumer<Object> block) {
    }

    void ifError(Consumer<Throwable> block) {
    }

    static class Success extends ScriptResult {
        private final Object returnValue;

        Success(Object returnValue) {
            this.returnValue = returnValue;
        }

        @Override
        void ifSuccess(Consumer<Object> block) {
            block.accept(returnValue);
        }
    }

    static class Error extends ScriptResult {
        private final Throwable throwable;

        Error(Throwable throwable) {
            this.throwable = throwable;
        }

        @Override
        void ifError(Consumer<Throwable> block) {
            block.accept(throwable);
        }
    }
}
抽象类脚本结果{
无效ifSuccess(消费者区块){
}
无效输入错误(用户块){
}
静态类成功扩展了ScriptResult{
私人最终目的价值;
成功(对象返回值){
this.returnValue=returnValue;
}
@凌驾
无效ifSuccess(消费者区块){
块。接受(返回值);
}
}
静态类错误扩展了ScriptResult{
私人最终可丢弃;
错误(可丢弃可丢弃){
this.throwable=throwable;
}
@凌驾
无效输入错误(用户块){
块。接受(可丢弃);
}
}
}
My kotlin测试使用以下断言助手:

private lateinit var scriptResult: ScriptResult

inline fun <reified T : Throwable> shouldHaveThrown(): T {
    scriptResult.ifSuccess { result ->
        fail("should have thrown ${T::class.java.name}, but returned `$result´")
    }
    lateinit var holder: T // (1)
    scriptResult.ifError { throwable ->
        if (throwable is T) {
            holder = throwable // (2)
        } else {
            fail("expected ${T::class.java.name} to be thrown, but threw `$throwable´")
        }
    }
    return holder // (3)
}
private lateinit var scriptResult:scriptResult
inline fun shouldhavehown():T{
scriptResult.ifSuccess{result->
失败(“应该抛出${T::class.java.name},但返回`$result´”)
}
lateinit var持有人:T/(1)
scriptResult.ifError{可丢弃->
如果(可丢弃为T){
支架=可丢弃/(2)
}否则{
失败(“应抛出${T::class.java.name},但抛出`$throwable´”)
}
}
报税表持有人//(3)
}
断言助手是有效的。我可以这样使用它:

    val thrown = execution.shouldHaveThrown<MissingMethodException>()
    assertThat(thrown.message).contains("missedMethod")
val-thrown=execution.shouldhavehrown()
assertThat(抛出.message).contains(“missedMethod”)

但我怀疑有一种更为惯用的方法可以从
shouldlhavebound
返回
throwable
,然后(1)声明持有者,(2)分配持有者,(3)最终返回持有者。如何?

为什么它甚至是一个列表?Java代码是可变的吗?我看到了另一种方法:使用
return@shouldHaveThrown
at(2)和
at(3)失败,或者使用
lateinit
,但它们仍然是黑客。我认为没有一种干净的方法,因为编译器无法知道
ifSuccess
ifError
是否会执行它们的块。@TimCastelijns当然你是wright。不需要列表。我更新了问题