Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Kotlin perform automatic type-casting?

Tags:

casting

kotlin

var a : Double
a = Math.sin(10) // error: the integer literal does not conform to the expected type Double
a = Math.sin(10.0) //This compiles successfully
println(a)

Why doesn't kotlin perform implicit type conversion and force us to pass the exact type of data?

fun sin(value: Double): Double // at kotlin documentation
like image 854
JavaBanana Avatar asked May 20 '17 02:05

JavaBanana


People also ask

Which type casting is automatically performed?

Widening Type Casting It is done automatically. It is safe because there is no chance to lose data. It takes place when: Both data types must be compatible with each other.

Does casting happen automatically?

It happens automatically when converting from a narrower range data type to a wider range data type. It also means converting a lower data type like an int to a higher data type like a double .

How do I cast a datatype in Kotlin?

In Smart Casting, we generally use is or !is an operator to check the type of variable, and the compiler automatically casts the variable to the target type, but in explicit type casting we use as operator. Explicit type casting can be done using : Unsafe cast operator: as.

What is the difference between automatic type conversion and type casting?

Type Conversion example – In type casting, a data type is converted into another data type by a programmer using casting operator. Whereas in type conversion, a data type is converted into another data type by a compiler. 2. Type casting can be applied to compatible data types as well as incompatible data types.


2 Answers

We all know that Kotlin has both non-nullable Int and nullable Int?.

When we use Int? this happens: Kotlin actually 'boxes' JVM primitives when Kotlin needs a nullable reference since it's aimed at eliminating the danger of null references from code.

Now look at this: (assuming this is a compilable code)

val a: Int? = 1
val b: Long? = a

Kotlin doesn't perform implicit type conversion because of this thing happens. If Kotlin did implicit type conversions, b should be 1. but since a is a boxed Int and b is a boxed Long, a == b yields false and falls into contradiction, since its == operator checks for equals() and Long's equals() checks other part to be Long as well.

Check the documentation:

  • Explicit Conversions in Kotlin
  • https://kotlinlang.org/docs/reference/basic-types.html
  • https://kotlinlang.org/docs/reference/equality.html
like image 138
shiftpsh Avatar answered Nov 05 '22 03:11

shiftpsh


Kotlin does not allow implicit conversions of numeric types. There is a misconception that implicit conversions are "no harm, no foul" ... which is wrong.

The process in Java for implicit conversions is more complicated than you think, read the docs to see what all is entailed. And then you can try to analyze all of the cases that can go wrong.

Kotlin, does not want the compiler to guess as to your intention, so it makes everything in the language explicit, including numeric type conversions. As explained in the Kotlin docs for Explicit Conversions it says clearly:

Due to different representations, smaller types are not subtypes of bigger ones. [...] As a consequence, smaller types are NOT implicitly converted to bigger types. [...] We can use explicit conversions to widen numbers.

And the documentation shows one such sample of where things can go wrong, but there are many others.

Nor can you just cast one numeric type to another, as mentioned here in incorrect comments and answers. That will only result in a nice runtime error. Instead look at the numeric conversion functions such as toInt() and toDouble() found on the numeric types, such as on the Number class.

Explicitness is part of the Kotlin personality, and it is not planned to change.

like image 40
Jayson Minard Avatar answered Nov 05 '22 05:11

Jayson Minard