Implicit type conversion: Difference between revisions

Added Kotlin
(Added Kotlin)
Line 476:
 
For reference, jq's builtin types are "number", "boolean", "null", "object" and "array".
 
=={{header|Kotlin}}==
In general, the only implicit type conversions allowed in Kotlin are between objects of sub-types and variables of their ancestor types. Even then, unless 'boxing' of primitive types is required, the object is not actually changed - it is simply viewed in a different way.
 
If follows from this that there are no implicit conversions (even 'widening' conversions) between the numeric types because there is no inheritance relationship between them. However, there is one exception to this - integer literals, which would normally be regarded as of type Int (4 bytes) can be assigned to variables of the other integral types provided they are within the range of that type. This is allowed because it can be checked statically by the compiler.
<lang scala>// version 1.1.2
 
open class C(val x: Int)
 
class D(x: Int) : C(x)
fun main(args: Array<String>) {
val c: C = D(42) // OK because D is a sub-class of C
println(c.x)
val b: Byte = 100 // OK because 100 is within the range of the Byte type
println(b)
val s: Short = 32000 // OK because 32000 is within the range of the Short Type
println(s)
val l: Long = 1_000_000 // OK because any Int literal is within the range of the Long type
println(l)
val n : Int? = c.x // OK because Int is a sub-class of its nullable type Int? (c.x is boxed on heap)
println(n)
}</lang>
{{out}}
<pre>
42
100
32000
1000000
42
</pre>
 
=={{header|Lua}}==
9,482

edits