If `Byte` is a `Number`, then why isn't a `Bit` /`...
# announcements
h
If
Byte
is a
Number
, then why isn't a
Bit
/`Boolean`? Iirc,
Boolean
isn't actually stored with just one bit in memory, but does that actually matter?
Sure but isn't a
Boolean
logically equivalent to a
Bit
, which is a number
s
I guess the same reason a
Char
is not a
Number
h
What do non-ascii characters return when you call
Char::toInt
?
For whatever reason a
Byte
is a number, the same can be applied to a
Bit
I'm saying
Bit
does exist in Kotlin. it's just called
Boolean
A Bit is a 0 or 1. Logical operations with bits are logically equivalent to those performed with booleans. If two things are logically identical, they are the same thing.
but in bit arithmetic 1==true, 0==false. all digital computation is based on Boolean logic by way of bit arithmetic. in bit arithmetic a bit is a True/False value.
Why are logical and numerical mutually exclusive?
I apologize if I sound combative. I'm not arguing to win, more to explore a concept
I guess my point is there need not be such a distinction. It is the same point as "Why does Kotlin/Java treat the boolean
&&
differently from the bitwise
and/&
? Shouldn't
true xor false
compile? It doesn't because true and false are not bitwise values, but in boolean logic if two statements have identical truth tables, they are logically equivalent; bitwise
and
and logical
&&
have identical truth tables. Same goes for all bitwise and boolean unary and binary functions
I guess I also question the lack of boolean xor, nor, nand in the language, too
the only one of which that is actually felt missing however is
xor
.
There's a
Boolean::xor
?
I know there's a bitwise one
oh shit
my bad. I guess I just assumed it didn't exist since
and
and
or
are exclusive to bitwise operations
🤫 That line is so blurry because it actually doesn't exist...
Should they hash to the same value?
I mean. 1L !== 1 We're already pretty comfortable with different number types
Boolean
would just extend
Number
and be comparable to other `Number`s.
I actually don't know what
===
vs
==
distinction is in kotlin. I initially assumed
===
meant equal hashcodes or that they must be the same javaclass, and that
==
was using
compareTo
operator, but that is not the case.
s
===
compares the instance reference.
==
executes the
equals
method
h
therefore the only way to use
operator compareTo
to compare equivalency between 2 instances is to do
x.compareTo(y) == 0
? That's pretty silly
s
True… for primitives,
==
and
===
are the same
No,
==
should suffice for equivalency.
h
Well I know the following doesn't compile:
Copy code
when(myLong) {
    0 -> ... // type mismatch Int/Long
    1 -> ... // type mismatch Int/Long
    else -> ...
}
s
Hmmm… I thought
equals
takes an
Any?
as input parameter….
h
🤷‍♂️ I tried it today
s
This maybe an additional check for a
when
clause by the compiler…. Wonder if the
Number#equals
enforces the receiver and the input parameter to be of the same class.
True, but Kotlin is not just JVM…
h
s
Something odd going on with Numbers with regards to `equals`… You can replace
myLong == 0
, which won’t compile with
myLong.equals(0)
, which does compile, but still complains
Going back to the JVM, this is how
equals
is implemented for a java.lang.Long:
Copy code
public boolean equals(Object obj) {
        if (obj instanceof Long) {
            return value == ((Long)obj).longValue();
        }
        return false;
    }
If the input parameter is not a
Long
, it will return false. Since it will always be false,
myLong == myInt
will not compile, since it always will return
false
h
Oh, and @streetsofboston
Char
is a
Number
in Kotlin
s
Copy code
public class Char private constructor() : Comparable<Char>
h
Oh, actually it isn't
i got confused because all `Number`s must include
toChar
thank you