Fuzion Logo
flang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.
Fuzion LanguageTutorialType casts

Type casts

TBW! Not supported yet.

Idea: A feature can define an inner feature as that converts its value to a given type. For loss-less conversions, these might be marked as implicit to permit the compiler to add these casts automatically:

  i32
  {
    as i8 require -128 <= i32.this <= 127; => castToByte(i32.this);
    as i16 require -0x8000 <= i32.this <= 0x7fff; => castToShort(i32.this);
    implicit as i64  => castToLong(i32.this);
    implicit as i128 => castToDoubleLong(i32.this);
  }

  x i32 = some i32;

  b1 i8   = x as i8;   // explicit call to "as i8"
  s1 i16  = x as i16;  // explicit call to "as i8"
  l1 i64  = x as i64;  // explicit call to "as i64"
  L1 i128 = x as i128; // explicit call to "as i128"

  b2 i8   = x;  // compile time error, no implicit conversion found
  s2 i16  = x;  // compile time error, no implicit conversion found
  l2 i64  = x;  // ok, implicit call to "as i64"
  L2 i128 = x;  // ok, implicit call to "as i128"

next: Conditional Statement