Fuzion Logo
fuzion-lang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.

Casts: Value / Numeric Types

There are basically two kinds of type casts. First, those that change the static type of an expression or reference type but do not change the value itself only the way it is handled by the compiler. (In Java the are implemented by the checkcast bytecode with the possibility to throw a runtime exception.) Second, type conversions such as (float) Math.PI that may change the value they operate on (as to a lower precision value in this case).

Casts in other Languages

Scala has implicit conversion functions: Scala Implicit Conversions.

Java has implicit conversions for numeric types, requires explicit conversions if precision could get lost. No other implicit or explicit conversions are supported, these must be done via dedicated methods.

Go? Rust? Python?

Casting Values in Fuzion

Idea: use a postfix operation as type as follows

i i32 := 1234567890
l i64 := i as i64
f f32 := i as f32
d f64 := i as f64
s i16 := i as i16

and implement them in the corresponding features, in this case in i32

i32 is
  native as i16
  auto native as i64
  auto native as f64

For casts that do not lose information such as i32 to f64, a modifier auto indicates that the compiler can call these automatically to make sure values are assignable, i.e., the example above can be simplified as follows

i i32 := 1234567890
l i64 := i   // i32.as i64 implicitly called
f f32 := i as f32
d f64 := i   // i32.as f64 implicitly called
s i16 := i as i16

TBW: What syntax is used in Python/F#/Nim/etc.?