Unlike many other languages, Scala does not support the use of numbers or other kinds of values ββin contexts where the logical is expected. For example, none of the following lines compiles:
if (1) "foo" else "bar" if ("1") "foo" else "bar" if (List(1)) "foo" else "bar"
Some languages ββhave an idea of βββveracityβ that will be used here to determine if a condition is true, but Scala doesn't - if you want a boolean, you need to use Boolean .
This means that ordinary logical negation does not make sense for numbers, and ~ is something completely different - it gives you bitwise negation, which you do not want. Instead, it seems likely that you want something like this:
val x = 1 val nonzeroX = x != 0 val y = !nonzeroX
Ie, you explicitly convert your number to a boolean, and then work with it using standard boolean negation ( ! ).
source share