https://kotlinlang.org logo
#stdlib
Title
# stdlib
h

holgerbrandl

11/01/2021, 7:39 PM
Just of curiosity: Why is
ceil()
not an extension on Double?
c

CLOVIS

11/01/2021, 8:06 PM
You're rounding the number, the number isn't rounding itself
Extension functions mean “this object can do [...]”, but rounding is something that happens to a number, not really something a number does
h

holgerbrandl

11/01/2021, 8:12 PM
Ah, thanks.
j

jimn

11/01/2021, 9:26 PM
one might expect extra jvm autobox penalties on delegates to Number hierarchy not that it isn't tempting to have a lot of sugar around
1
e

ephemient

11/01/2021, 10:47 PM
none of the
kotlin.math
functions operate on generic
Number
, only concrete
Int
Long
Float
Double
primitive types
val Double.absoluteValue
doesn't box
j

jimn

11/01/2021, 11:20 PM
context is important. delegates work on vtables; HashSet<Int> is simply not going to be the same as eclipse IntHashSet
e

ephemient

11/01/2021, 11:22 PM
that's true, but how is that related to this thread?
j

jimn

11/02/2021, 12:48 AM
one might expect extra jvm autobox penalties on delegates to Number hierarchy, period, under all circumstances, using the jvm. kotlin or otherwise.
e

ephemient

11/02/2021, 12:56 AM
the existence of Number has no bearing on any of these functions and extensions
j

jimn

11/02/2021, 1:10 AM
a more interesting point is that syntactic sugar increases the evaluation size of inlining/EA candidates toward the tiny default threshold set for java language. doubleIterator.map{it.ceil()} <--- requires extra love.
e

ephemient

11/02/2021, 1:25 AM
DoubleIterator.map
is neither inline, and even if it were, it is not specialized so https://youtrack.jetbrains.com/issue/KT-29460 results in boxing. so that still makes no difference
now, if you were talking about a hypothetical specialized
DoubleIterable.map
, that could be different… but it also doesn't exist
j

jimn

11/02/2021, 5:14 AM
srcDoubleArray.let{src->DoubleArray(src.size){ceil(src[it])}} stands a better chance than most things to minimize boxing if ceil is just aliased from java.
2 Views