Smallville7123
04/18/2019, 12:18 PMNumber
for example fun Number.comparisionsGreaterThan(i: Number): Boolean {
return this < i
}
wbertan
04/18/2019, 12:21 PMNumber
doesn't implement Comparable
./*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* <http://www.apache.org/licenses/LICENSE-2.0>
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlin
/**
* Superclass for all platform classes representing numeric values.
*/
public abstract class Number {
/**
* Returns the value of this number as a [Double], which may involve rounding.
*/
public abstract fun toDouble(): Double
/**
* Returns the value of this number as a [Float], which may involve rounding.
*/
public abstract fun toFloat(): Float
/**
* Returns the value of this number as a [Long], which may involve rounding or truncation.
*/
public abstract fun toLong(): Long
/**
* Returns the value of this number as an [Int], which may involve rounding or truncation.
*/
public abstract fun toInt(): Int
/**
* Returns the [Char] with the numeric value equal to this number, truncated to 16 bits if appropriate.
*/
public abstract fun toChar(): Char
/**
* Returns the value of this number as a [Short], which may involve rounding or truncation.
*/
public abstract fun toShort(): Short
/**
* Returns the value of this number as a [Byte], which may involve rounding or truncation.
*/
public abstract fun toByte(): Byte
}
public class Int private constructor() : Number(), Comparable<Int> { ... }
Stephan Schroeder
04/18/2019, 12:56 PMfun Number.comparisionsGreaterThan(i: Number) = this.toDouble() < i.toDouble()
Smallville7123
04/18/2019, 1:26 PMNumber
in such that all information is preserved and nothing gets truncated as a result?Ruckus
04/18/2019, 2:18 PMNumber
isn't comparable, why Kotlin doesn't do automatic number conversions, and why different number types exist in the first place.val x = 21412415121234567L
val y = x + 1
println(x.toDouble() < y.toDouble())
> false
Smallville7123
04/18/2019, 2:27 PMStephan Schroeder
04/18/2019, 2:39 PMRuckus
04/18/2019, 2:42 PMDouble is the type with the highest resolutionThat's simply not true. In my example, leaving the values as
Long
would return true.Stephan Schroeder
04/18/2019, 2:42 PMRuckus
04/18/2019, 2:46 PMNaN
isn't actually comparable to anything.Stephan Schroeder
04/18/2019, 2:47 PMkarelpeeters
04/18/2019, 3:03 PMNumber
implementations.Number
, I'd try to avoid using it at all.Smallville7123
04/18/2019, 3:21 PMNumber
karelpeeters
04/18/2019, 3:24 PMSmallville7123
04/18/2019, 3:24 PM<
karelpeeters
04/18/2019, 3:24 PM.compareTo
.Smallville7123
04/18/2019, 3:25 PM<=
>
>=
==
!=
karelpeeters
04/18/2019, 3:26 PMfun <T: Comparable<T>> T.comparisionsGreaterThan(other: T): Boolean {
return this < other
}
Smallville7123
04/18/2019, 3:26 PMisNotEqualTo
to !=
which resolves to .compareTo
karelpeeters
04/18/2019, 3:27 PMSmallville7123
04/18/2019, 3:27 PMkarelpeeters
04/18/2019, 3:27 PMSmallville7123
04/18/2019, 3:27 PMkarelpeeters
04/18/2019, 3:28 PMSmallville7123
04/18/2019, 3:28 PMkarelpeeters
04/18/2019, 3:28 PMNumber
, but that's kind of shady:
fun <T> T.comparisionsGreaterThan(other: T): Boolean where T : Comparable<T>, T: Number {
return this < other
}
Smallville7123
04/18/2019, 3:30 PMinfix fun <T: Comparable<T>> T.isGreaterThan(i: T) = this > i
infix fun <T: Comparable<T>> T.isGreaterThanOrEqualTo(i: T) = this >= i
infix fun <T: Comparable<T>> T.isLessThan(i: T) = this < i
infix fun <T: Comparable<T>> T.isLessThanOrEqualTo(i: T) = this <= i
infix fun <T: Comparable<T>> T.isEqualTo(i: T) = this == i
infix fun <T: Comparable<T>> T.isNotEqualTo(i: T) = this != i
karelpeeters
04/18/2019, 3:33 PM: Boolean
, {}
, and return
.Smallville7123
04/18/2019, 3:35 PMkarelpeeters
04/18/2019, 3:36 PMSmallville7123
04/18/2019, 3:36 PMkarelpeeters
04/18/2019, 3:36 PMSmallville7123
04/18/2019, 3:37 PMkarelpeeters
04/18/2019, 3:37 PMSmallville7123
04/18/2019, 3:38 PMkarelpeeters
04/18/2019, 3:38 PM>
thing wanting to "eat" as much as possible, so 2 < 5
because it wants to eat the 5
.Ruckus
04/18/2019, 3:39 PMkarelpeeters
04/18/2019, 3:40 PMSmallville7123
04/18/2019, 3:41 PMRuckus
04/18/2019, 3:45 PMkarelpeeters
04/18/2019, 3:46 PMRuckus
04/18/2019, 3:46 PM