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

Type.fz


# This file is part of the Fuzion language implementation.
#
# The Fuzion language implementation is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, version 3 of the License.
#
# The Fuzion language implementation is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License along with The
# Fuzion language implementation.  If not, see <https://www.gnu.org/licenses/>.


# -----------------------------------------------------------------------
#
#  Tokiwa Software GmbH, Germany
#
#  Source code of Fuzion standard library feature Type
#
#  Author: Fridtjof Siebert (siebert@tokiwa.software)
#
# -----------------------------------------------------------------------

# Type -- parent feature of all type features
#
# type features 'f.type' are declared implicitly for every feature f.
# Type features do not contain state, they are unit types.
#
# All type features inherit directly (Any.type) or indirectly (all
# others type features) from this feature.
#
module:public Type ref is


  # name of this type, including type parameters, e.g. 'option (list i32)'.
  #
  public name String => intrinsic


  # string representation of this type to be used for debugging.
  #
  # result has the form "Type of '<name>'", but this might change in the future
  #
  public redef as_string => "Type of '$name'"


  # There is no dynamic type of a type instance since this would result in an
  # endless hierachy of types, so dynamic_type is redefined to just return
  # Type.type here.
  #
  public redef dynamic_type => Type.type


# Types -- features related to Type but not requiring an instance of Type
#
public Types is

  # Get the Type instance corresponding to a given type
  #
  # The result of 'Types.get x' is the same as 'x.type'.
  #
  # Internally, Fuzion's front end implements 'x.type' using
  # 'Types.get x'.
  #
  public get(T type) => T


# universe feature to determine the compile-time type of an expression.
#
# This is to be called without an actual type passed to `T`, but `T` should be
# inferred from the actual value argument `_`.
#
# The value arugment is evaluated and ignored.
#
# The result is the type of the value argument boxed into a ref value and returned
# as a value of type `Type`.
#
# examples:
#
#   `type_of "bla"` is `String`
#   `type_of (panic "***")` will terminate
#
public type_of(T type, _ T) => T


# universe feature to determine the compile-time type of an expression.
#
# This is to be called without an actual type passed to `T`, but `T` should be
# inferred from the result type of the actual value argument `_`.
#
# The function passed as value arugment is ignored, it is not called.
#
# The result is the result type of the value argument boxed into a ref value and
# returned as a value of type `Type`.
#
# examples:
#
#   `type_of_lazy ()->"bla"` is Type of `String`
#   `type_of_lazy ()->(panic "***")` is Type of `void`.
#
# NYI: Fuzion should have better syntax sugar for lazy value arguments such that
# we do not need the `()->` prefix to create a lambda.  Then, `type_of` could
# have a lazy argument and `type_of_lazy` could be removed.
#
public type_of_lazy(T type, _ ()->T) => T