holgerbrandl
01/16/2021, 8:20 PMfun foo(param:Int) {
// old school, but could we use a contract here to get compiler error at callsite for foo(-1) instead?
require(param >1)
// do something great
println(param)
}
dmitriy.novozhilov
01/16/2021, 8:22 PMholgerbrandl
01/16/2021, 8:22 PMholgerbrandl
01/16/2021, 8:23 PMdmitriy.novozhilov
01/16/2021, 8:24 PMholgerbrandl
01/16/2021, 8:30 PMdmitriy.novozhilov
01/16/2021, 8:43 PMfoo(-1)
but call something like
val x = receiveDataFromSomewhere()
foo(x)
And in such situations simple contracts like foo requires that x > 0
won't work. To handle this you also need some function that provides information that some value is really > 0
, which may add a lot of verbosity (imagine that you always need to check that index >= 0
before calling list[index]
)
I can agree that it can be useful in some situations, but currently it's not in our priorities.holgerbrandl
01/16/2021, 8:45 PMdmitriy.novozhilov
01/16/2021, 8:46 PMraulraja
01/16/2021, 9:57 PMholgerbrandl
01/17/2021, 8:54 AMelizarov
01/20/2021, 1:16 PMinline class PositiveInt(val value: Int) {
init { require(value > 0) }
}
// Now, you can be 100% sure!
fun foo(param: PositiveInt)
elizarov
01/20/2021, 1:17 PM