I would like to write the following in Scala:
def method(a: Float, b: Float, c: Float) = {
if( a < b <= c) {
...
}
}
Currently, this is not valid. Indeed, a < b
returns a Boolean, which for comparison purposes is wrapped by a booleanWrapper
. The compiler then complains that c is of type Float
and not Boolean
, so it does not compare b
to c
at all.
Would it be possible to use implicit classes, methods and value classes to be able to achieve this?
For now, I have only been able to do the following:
class Less(val previous: Boolean, last: Float) {
def <=(other: Float): Less = new Less(previous && last <= other, other)
def <(other: Float): Less = new Less(previous && last < other, other)
}
implicit def f(v: Float): Less = {
new Less(true, v)
}
implicit def lessToBoolean(f: Less): Boolean = {
f.previous
}
def method(a: Float, b: Float, c: Float) = {
if( f(a) < b <= c) {
...
}
}
Any way to remove this f using standard tricks?