Skip to content

Commit f9efdd9

Browse files
committed
Clarify implicit conversion rules
1 parent ecd27ea commit f9efdd9

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

docs/reference/types.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ fun CanReassign(var str: String): Void {
220220
## Type System Characteristics
221221

222222
**Static typing:** Every variable and expression has a type checked at compile time
223-
**No implicit conversions except builtin wrappers:** Explicit casting required between different types, except for conversions between primitive reference types and their corresponding fundamentals.
223+
**Limited implicit conversions:** The compiler only performs implicit conversions between a primitive reference type and its matching fundamental (for example, `Int``int`). Any conversion across different primitive families—such as `Int` to `Float` or `Float` to `int`—must use an explicit cast.
224224
**Type safety:** Prevents many common errors
225225
**Nullable types:** Any reference type (including primitive reference types) can be made nullable by appending `?`. Fundamental types cannot be nullable.
226226

@@ -233,11 +233,11 @@ val intVal: int = 42
233233
val floatVal: float = 3.14
234234
val result: float = (intVal as float) + floatVal // OK: Explicit conversion
235235
236-
// Using primitive reference types (implicit conversion)
237-
val refInt: Int = 42 // Implicit conversion from literal
238-
val refFloat: Float = 3.14 // Implicit conversion from literal
239-
val sum: Int = refInt + (refFloat as Int) // Explicit conversion
240-
val fundamentalSum: int = sum + 10 // Implicit: Int + int -> int
236+
// Using primitive reference types (implicit conversion between wrappers and fundamentals)
237+
val refInt: Int = 42 // Implicit conversion from literal to Int
238+
val refFloat: Float = 3.14 // Implicit conversion from literal to Float
239+
val sum: Int = refInt + (refFloat as Int) // Requires explicit narrowing
240+
val fundamentalSum: int = sum + 10 // Implicit: Int -> int when assigning to a fundamental
241241
242242
// Converting nullable primitives to fundamentals
243243
val nullableInt: Int? = 42 // Implicit conversion from literal

0 commit comments

Comments
 (0)