有一个getter返回一个不可为空的类型,即使该后台字段是可空的(Having a getter return a non-nullable type even though the backing field is nullable)

num在设置时应该为空,但它返回的值应始终为非空(具有默认值)。

class Test { var num: Int? = null get() = field ?: 5 // default value if null }

即使返回的值总是非空,这对我来说也是合理的,因为这种类型不是推断出来的,而是从后台字段中取得的:

val a: Int = Test().num

类型不匹配:推断类型是Int? 但国际预期

问题是如何将该getter的返回类型更改为不可为空? 如果我这样做,编译器会说:

Getter返回类型必须等于属性的类型,即'Int?'


我知道我可以用另一个属性numNotNullable (没有后台字段)解决它。

class Test { var num: Int? = null get() = field ?: 5 // default value if null val numNotNullable: Int get() = num ?: 5 } val c: Int = Test().numNotNullable

但这不是我想要的。 有另一种方法吗?

num should be nullable when set, but what it returns should always be non-nullable (have a default value).

class Test { var num: Int? = null get() = field ?: 5 // default value if null }

The following does not compile even though the returned value is always non-null which makes sense to me, because the type is not inferred but taken from the backing field:

val a: Int = Test().num

Type mismatch: inferred type is Int? but Int was expected

The question is how can I change the return type of that getter to be non-nullable? If I do so, the compiler says:

Getter return type must be equal to the type of the property, i.e. 'Int?'


I know that I could solve it with another property numNotNullable (without a backing field).

class Test { var num: Int? = null get() = field ?: 5 // default value if null val numNotNullable: Int get() = num ?: 5 } val c: Int = Test().numNotNullable

But this is not what I want. Is there another way?

最满意答案

var num: Int? = null

这是你的财产签名。 没关系,如果你在内部确保没有返回null值。 签名说,该值是可空的。

这涉及到:

您可以将null设置为该字段 所有使用此字段的类都必须处理该属性可以返回null的事实

第二个属性的解决方案很好。

您当然可以用普通的旧Java bean替换该属性,但我不会建议这样做,因为您必须使用getNumb和setNum访问prop。

class Test { private var num: Int = 5 fun setNum(num: Int?) { this.num = num ?: 5 } fun getNum() = num }

var num: Int? = null

This is your property signature. It doesn't matter, if you internally ensure that no null value is returned. The signature says, that the value is nullable.

This implicates:

You are allowed to set null to this field All classes using this field, must handle the fact that the property can return null

Your Solution with a second property is good.

You of course can replace the property with plain old java bean, but I wouldn't advise that, because than you have to access the prop with getNumb and setNum.

class Test { private var num: Int = 5 fun setNum(num: Int?) { this.num = num ?: 5 } fun getNum() = num }有一个getter返回一个不可为空的类型,即使该后台字段是可空的(Having a getter return a non-nullable type even though the backing field is nullable)

num在设置时应该为空,但它返回的值应始终为非空(具有默认值)。

class Test { var num: Int? = null get() = field ?: 5 // default value if null }

即使返回的值总是非空,这对我来说也是合理的,因为这种类型不是推断出来的,而是从后台字段中取得的:

val a: Int = Test().num

类型不匹配:推断类型是Int? 但国际预期

问题是如何将该getter的返回类型更改为不可为空? 如果我这样做,编译器会说:

Getter返回类型必须等于属性的类型,即'Int?'


我知道我可以用另一个属性numNotNullable (没有后台字段)解决它。

class Test { var num: Int? = null get() = field ?: 5 // default value if null val numNotNullable: Int get() = num ?: 5 } val c: Int = Test().numNotNullable

但这不是我想要的。 有另一种方法吗?

num should be nullable when set, but what it returns should always be non-nullable (have a default value).

class Test { var num: Int? = null get() = field ?: 5 // default value if null }

The following does not compile even though the returned value is always non-null which makes sense to me, because the type is not inferred but taken from the backing field:

val a: Int = Test().num

Type mismatch: inferred type is Int? but Int was expected

The question is how can I change the return type of that getter to be non-nullable? If I do so, the compiler says:

Getter return type must be equal to the type of the property, i.e. 'Int?'


I know that I could solve it with another property numNotNullable (without a backing field).

class Test { var num: Int? = null get() = field ?: 5 // default value if null val numNotNullable: Int get() = num ?: 5 } val c: Int = Test().numNotNullable

But this is not what I want. Is there another way?

最满意答案

var num: Int? = null

这是你的财产签名。 没关系,如果你在内部确保没有返回null值。 签名说,该值是可空的。

这涉及到:

您可以将null设置为该字段 所有使用此字段的类都必须处理该属性可以返回null的事实

第二个属性的解决方案很好。

您当然可以用普通的旧Java bean替换该属性,但我不会建议这样做,因为您必须使用getNumb和setNum访问prop。

class Test { private var num: Int = 5 fun setNum(num: Int?) { this.num = num ?: 5 } fun getNum() = num }

var num: Int? = null

This is your property signature. It doesn't matter, if you internally ensure that no null value is returned. The signature says, that the value is nullable.

This implicates:

You are allowed to set null to this field All classes using this field, must handle the fact that the property can return null

Your Solution with a second property is good.

You of course can replace the property with plain old java bean, but I wouldn't advise that, because than you have to access the prop with getNumb and setNum.

class Test { private var num: Int = 5 fun setNum(num: Int?) { this.num = num ?: 5 } fun getNum() = num }