最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

kotlin - Is there a way to pass smart castnull check out of the function? - Stack Overflow

programmeradmin1浏览0评论

Consider following two examples. The compiler in example1 can tell that the a.number is non-nullable, but if I wrap the check into function this information doesn't get passed out of it.

Is there a way in Kotlin to tell the compiler that factoryAWithNumber result contains a property that is not null? I am thinking something like contracts, but they don't seem to be able to do it.

data class A(
    val name: String,
    val number: Int?,
)

fun factoryAWithNumber(): A? {
    val a = A("2", 2) // A is obtained with unknown property values using another method
    if (a.number == null) {
        return null
    }
    return a
}

fun example1(): Int {
    val a = A("1", 1)
    if (a.number == null) {
        return -1
    }
    return a.number // Compiler knows number cannot be null
}

fun example2(): Int {
    val a = factoryAWithNumber() ?: return -1
    return a.number!! // Compiler does not know
}

Consider following two examples. The compiler in example1 can tell that the a.number is non-nullable, but if I wrap the check into function this information doesn't get passed out of it.

Is there a way in Kotlin to tell the compiler that factoryAWithNumber result contains a property that is not null? I am thinking something like contracts, but they don't seem to be able to do it.

data class A(
    val name: String,
    val number: Int?,
)

fun factoryAWithNumber(): A? {
    val a = A("2", 2) // A is obtained with unknown property values using another method
    if (a.number == null) {
        return null
    }
    return a
}

fun example1(): Int {
    val a = A("1", 1)
    if (a.number == null) {
        return -1
    }
    return a.number // Compiler knows number cannot be null
}

fun example2(): Int {
    val a = factoryAWithNumber() ?: return -1
    return a.number!! // Compiler does not know
}
Share Improve this question edited Jan 23 at 8:25 DarkBee 15.6k8 gold badges72 silver badges117 bronze badges asked Jan 22 at 14:37 MrKewMrKew 9895 silver badges20 bronze badges 2
  • 1 Search "Kotlin contracts" for details on how you can do it if the function takes a parameter, but not exactly how you've got it here. – k314159 Commented Jan 22 at 14:45
  • I see, I could do it if A was a parameter of the factory method. Hmm does not help with what I'm trying to do. I guess I am just lazy to create a new data class AWithNumber that copies all the properties from A but makes number non-nullable, and return that instead. I was just hoping the compiler could do it for me. – MrKew Commented Jan 23 at 8:24
Add a comment  | 

1 Answer 1

Reset to default 1

Not possible given there is nothing passed in to factoryAWithNumber(). (Currently) Contracts only can look at parameters or returns.

Think of your request this way: there is a block box that may return a value or null based on some hidden data. At compile time there is no possible way you can know which case it is, so you have to put in place a run-time test.

发布评论

评论列表(0)

  1. 暂无评论