https://kotlinlang.org logo
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
benchmarks
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
confetti
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
lincheck
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by
Title
h

Hullaballoonatic

06/14/2019, 12:24 AM
Why is
Number
not operable in the JVM? Why did Sun design it that way and why does JB not fix it in Kotlin? Everything (afaik) that inherits from
Number
is operable, so why wouldn't the operators be refactored to be included there?
s

Shawn

06/14/2019, 1:37 AM
What does
4 + NaN
equal?
h

Hullaballoonatic

06/14/2019, 3:09 AM
4
s

Shawn

06/14/2019, 3:10 AM
Why?
And is it 4 or is it 4.0?
’cause the two data types are disparate
h

Hullaballoonatic

06/14/2019, 3:10 AM
that or
error("You cannot add NaN to a number")
it's 4 if it's not rational
s

Shawn

06/14/2019, 3:12 AM
but you can’t have your return type vary like that, right?
4.0
,
NaN
, and
6.9
are all valid float values - you can’t vary the return type of a function based on the value of its arguments
h

Hullaballoonatic

06/14/2019, 3:12 AM
basically what I'm recommending is simply that the
Number
class has the following added:
abstract operator fun <N: Number> plus(other: N): N

// etc for others
then all the inheritors simply override that, instead of all individually implementing them
so if you add another type to your number, you must return that type
s

Shawn

06/14/2019, 3:14 AM
but to what end? adding them to the Number API means that you can reasonably perform those operations with two of any given number, which is decidedly not the case
h

Hullaballoonatic

06/14/2019, 3:15 AM
or that
Number
become
abstract class Number<N: Number<N>>()
and therefore you can only do the operations on a number of the same type
BUT
at least you could then make general number functions
i understand that you're just picking my brain, and getting me to consider all the cases, and I appreciate that.
s

Shawn

06/14/2019, 3:16 AM
what’s the point. though? this just obscures nuance between data types and creates room for subtle bugs and pseudo-nondeterministic behavior
h

Hullaballoonatic

06/14/2019, 3:16 AM
so why do essentially all other languages outside of jvm ones not have this problem?
s

Shawn

06/14/2019, 3:18 AM
I mean, I’m not much of a polyglot all things considered - what statically typed languages have a parent Number class its numeric datatypes all inherit from and can all be arbitrarily added (due to its supertype guaranteeing mathematical operations)?
bonus - what languages do so without ever losing precision or making overflow behavior ambiguous?
h

Hullaballoonatic

06/14/2019, 3:23 AM
I'm not actually asking for the compiler to try to optimize the number type
s

Shawn

06/14/2019, 3:24 AM
Who said anything about compiler optimizations?
h

Hullaballoonatic

06/14/2019, 3:24 AM
i'm only asking that the operations that they all share, that work exclusively with their own class, be refactored up into number, so that you always know that if you have an object of type number, whatever class it is an instance of, it has +,-,/,*,%,etc
so you can make generic functions like
fun adder<N: Number>(a: N, b: N): N = a + b
if you put doubles in there, you get a double, etc
s

Shawn

06/14/2019, 3:26 AM
I mean, you won’t know it’s a double really, it’d just be
Number
which means that 2+2 can no longer trivially be
(int) 4
h

Hullaballoonatic

06/14/2019, 3:27 AM
i see
s

Shawn

06/14/2019, 3:28 AM
there’s a lot of wacky stuff that could go on under the hood that really would not be obvious
what if some operations eventually coerced a
float
up to a
BigDecimal
?
if it’s all just
Number
throughout, that seems like a nightmare to debug
well, perhaps not debug in that case, maybe just profile in terms of performance
JavaScript “gets around” this by literally giving you nothing but IEEE 754 floating point (i.e. no integers or any other numeric data type unless you create them yourself)
also by being like “lol types”
In a strict sense… I think you could implement that generic
adder()
function as is - just with a lot of runtime typechecking
return when (lhs) {
  is Double -> when (rhs) {
    is Double -> lhs + rhs  // return a Double, but it's a Number™