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

Type checks and casts

Fuzion does not support type checks or type casts and there are currently no plans to add type casts. There are, however, means to convert simple numeric types through intrinsic features and there is a pattern that allows type checks and casts for dynamic (reference) types.

Type Casts for Numeric Types

Numeric conversions

The standard numeric types such as i32, u8, f64, u64, etc. provide features for numeric conversion where this is possible. These features are called as_<type>. The following example shows how these can be used:

These numeric conversions have in common that the value represented does not change significantly in these conversions, except for some rounding.

Numeric casts

Numeric casts are different in that they use the underlying bit-representation of a value and interpret these as a new type. There are features called cast_to_<type> to re-interpret the bit representation and features like low8bits to extract certain bits only:

Type Casts for Dynamic Types

Fuzion does not have a counterpart to Java's instanceof checks and the related type casts. The reason for this is that in a situation where explicit checks for a fixed number of dynamic types make sense are usually situations where the object-oriented approach that allows the definition of an arbitrary number of heirs does not not make sense. Code like this would be difficult to maintain since addition of new heirs may require adding of new cases. Instead, choice types and match match expressions are the preferred way to handle a fixed number of cases.

Consequently, a typical case of Java code like this

class A {
}

class B extends A {
}

A a := ...
if (a instanceof B b) {
  .. do something ..
}

would be done using a choice type in Fuzion:

B is
  do_b_things =>
    say "doing B's thing"

C is

a B | C := ...
match a
  b B => b.do_b_things
  _ C => say "nothing to be done"

Here is a running example using a choice type:

Inner features instead of type checks

Alternatively, type checks for heir types can be realized in Fuzion using the classic object-oriented approach using a redefined feature following this pattern:

  A is
    do_b_things is # do nothing

  B : A is
    redef do_b_things is
      # do something

An running example:

option and match instead of type checks

Alternatively, we could use an option and a match instead of the if .. instanceof .. as follows:

A is
  getB option B => nil

B : A is
  redef getB => B.this

a A := ...
match a.getB
  nil => # do nothing
  b B => b.do_b_things

An example using this pattern:

Note that it is even possible to add features like do_b_things or getB to an existing class imported from another module, so a library whose design does not fit our purpose can be extended accordingly.