Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't UInt have a toDouble()?

Tags:

kotlin

Consider:

val foo: Int = 1 foo.toDouble() // ok  val bar = 2.toUInt() bar.toDouble() // error! 

This doesn't make sense to me. Why wouldn't UInt have toDouble? (it also doesn't have .toFloat).

The docs say:

Every number type supports the following conversions:

  • toByte(): Byte
  • toShort(): Short
  • toInt(): Int
  • toLong(): Long
  • toFloat(): Float
  • toDouble(): Double
  • toChar(): Char

So it should be possible. The error I get is:

Error:(11, 4) Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: @InlineOnly public inline fun String.toDouble(): Double defined in kotlin.text 

Is UInt not considered a number type? Or is it something else?

like image 214
Rakete1111 Avatar asked Apr 11 '19 16:04

Rakete1111


2 Answers

This appears to be coming in 1.3.30, according to this YouTrack request.

1.3.30 was just recently tagged and appears to be releasing very shortly.

like image 143
Todd Avatar answered Oct 23 '22 00:10

Todd


Is UInt not considered a number type?

Yes, it doesn't extend Number class.

Declaration of Int:

class Int : Number, Comparable<Int> 

Declaration of UInt:

inline class UInt : Comparable<UInt> 

Starting with Kotlin version 1.3.30 UInt has toFloat and toDouble methods.

like image 27
Andrew Churilo Avatar answered Oct 23 '22 00:10

Andrew Churilo