Could some contract be added to `Int.sign` to supp...
# stdlib
a
Could some contract be added to
Int.sign
to support this?
Copy code
val foo = when (a.compareTo(b).sign) {
    -1 -> SomeEnum.LEFT
    0 -> SomeEnum.NONE
    1 -> SomeEnum.RIGHT
}
Does the contract API support something like this?
k
I don't think the current contract system supports this, maybe suggest it on the KEEP tracking issue? A related language feature I'd like is to make
when
smarter and have it consider this to be exhaustive:
Copy code
val x: Int
val s = when {
    x < 0 -> -1
    x == 0 -> 0
    x > 0 -> 1
}
👍 3
m
This can be solved without special compiler features by returning
enum
from
sign
a
Int.sign
is in stdlib, that’s why I posted in #stdlib
I don’t expect they are going to change its return type
k
Thinking about it more this really would be an anti-feature, you're supposed to use an enum instead. Of course it's too late for that in this case.
a
But sometimes you need
sign
to return an
Int
, for example it can appear in some math formulas
k
x.sign.toInt()
simple smile
But yeah the current choice makes sense as well.