Comparable<Boolean> should be usable in if-statements

Classes that implement Comparable<Boolean> should be able to be used as boolean expressions (or at least in if-statements)

class Test: Comparable<Boolean> {
    /* ... */

fun main() {
  val x = Test()

  if (x) {

A if-statement compares something to true, and Comparable is meant for comparisions

if doesn’t compare anything, it receives a simple boolean value and we have to perform any comparing on our side. If we do if (x < 3) then comparing is not done by the if statement, but by our code.

Also, Comparable is meant mostly for sorting. It is more complicated than simple true/false value. It is not entirely required (although recommended) to be consistent with equals(). Technically speaking, it is possible it returns 0 for both true and false (meaning it is both true and false) or return e.g. 1 for both of them (neither true nor false).

Having said that, I see a need to sometimes use an object as a boolean, but I don’t think we should use Comparable for this - it is designed for something much different. I would prefer a specialized operator for this feature. Or even a generic operator for implicit conversion to various types, not only boolean.


Makes sense.
Yeah, I want to make a class that behaves like a boolean, so some way to do that would be nice.

Maybe this is interesting for you:

interface Booleable {
    fun toBool(): Boolean

inline fun ifb(condition: Booleable, thenBlock: () -> Unit) {
    if (condition.toBool()) {

With a variable b of any type that implements Booleable, you can call:

ifb (b) {
    // do whatever should be done if b is true
    println("is true")

Yes that works but something like implicit casting to booleans would be really cool