尽管如此,在所有路径上都没有返回的自定义getter仍会编译 [英] Custom getter without return on all paths compiles nevertheless

查看:78
本文介绍了尽管如此,在所有路径上都没有返回的自定义getter仍会编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这段代码:

var test: String? = null
    get() {
        field?.also {
            return "has value"
        }
    }

仅当该字段为非空时,它会到达return.否则,身体就完成了.尽管如此,如果field为null,则可以正常编译并返回null.

It reaches return only when the field is non-null. Otherwise the body just completes. Nevertheless, this compiles fine and returns null if field is null.

如果我改成这个:

var test: String? = null
    get() {
        if (field != null)
            return "has value"
    }

现在,编译器抱怨该块主体需要一个return语句.

now the compiler complains that the block body needs a return statement.

这是一些未记录的功能还是错误?

Is this some undocumented feature or a bug?

推荐答案

这确实是一个错误,计划在1.3.20中修复.

This is indeed a bug, planned to be fixed in 1.3.20.

这篇关于尽管如此,在所有路径上都没有返回的自定义getter仍会编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆