Michael Sim
04/22/2020, 6:56 PMvar x = 10
var z: Long = x.toLong()
// or can I just write var z as:
var z = x.toLong()
// compiler should be able to infer the type, no?
Shawn
04/22/2020, 6:58 PMIs it necessary to specify the type in this caseno
yes// compiler should be able to infer the type, no?
Shawn
04/22/2020, 6:58 PMMichael Sim
04/22/2020, 7:00 PMMichael Sim
04/22/2020, 7:02 PMMichael Sim
04/22/2020, 7:02 PMShawn
04/22/2020, 7:02 PMShawn
04/22/2020, 7:03 PMShawn
04/22/2020, 7:04 PMMichael Sim
04/22/2020, 7:05 PMShawn
04/22/2020, 7:07 PMval foo = 42.toLong()
println(foo::class) // class kotlin.Long
Michael Sim
04/22/2020, 7:11 PMShawn
04/22/2020, 7:12 PMLong
and are specifying a literal for it, you don’t have to call .toLong()
- just append L
to it: val foo = 42L
. integer literals that are greater than 2³¹ - 1
are automatically allocated as Long
s — see the docs: https://kotlinlang.org/docs/reference/basic-types.htmlShawn
04/22/2020, 7:12 PMMichael Sim
04/22/2020, 7:19 PM