```var x: Number = 2 var y: Number = 3 var c = x ...
# getting-started
r
Copy code
var x: Number = 2
var y: Number = 3

var c = x + y
it is so annoying that this is not accepted in kotlin
🚫 2
j
Why do you use the
Number
type?
r
@Joffrey because the vast majority of the time that I am annotating a function parameter or a class field as something numerical, it is indifferent to whether it is a float or integer
most high level programming does not care about this distinction
i want to be able to do
fn(5)
bu also
fn(5.9)
and kotlin does not accept
x: Int | Double
union types
I'll be interfacing with some game framework and want to type
player.health -= 5
and then it goes "woops! you need to use a double!"
🚫 2
so I have to go
player.health -= 5.0
and that is so pedantic.
j
If you don't care about the difference, why not use a float or double in the first place?
r
if I use a double, then that forces all of the consumers of my api to pass in a double later
imagine I have some function that returns an integer
and for whatever resaon I want to pass the result of that function to some gameplay logic that moves a player
but I annotated that player class as using doubles
now there is this stupid conversion step I need to do
i end up having to make literally all of my codebase default to doubles
just in case i might want to interface between a module that uses doubles
j
and then it goes "woops! you need to use a double!"
It doesn't. You can totally do this. https://pl.kotl.in/nnmxU1HGP
r
oh I was wrong about that
i see. it must have been when I had this var x: Double var y: Number = 5 x + y
you cant add a Number to a double
j
What would this operation return? In general you should never use
Number
for your types, if your function manipulates numbers that can be non-integers, just use double or float, so the operations you do are clearly defined in terms of overflow and return type. If you're fine with languages like JS that don't distinguish different number types, then you pretty much get doubles everywhere, so the argument about ints is moot. In general though, we want to be aware of the precision or overflows of the operations we perform. So we use distinct types and explicit conversions where it makes sense
âž• 2
a
But most dynamic language actually DO care about that distinction. But they bury it under implied assumptions leading to a world of pain.
👆 3
l
You actually can also create an extension operator function for Number.plus(Number) that will explicitly define how the different types interact using a when expression.
âž• 1
c
the vast majority of the time that I am annotating a function parameter or a class field as something numerical, it is indifferent to whether it is a float or integer
As long as you have that attitude, please don't work in finance or any other critical fields. And read a bit on floating point arithmetic.