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

eff/fallible.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 eff.fallible
#
# -----------------------------------------------------------------------

# fallible -- fallible is a generic type for effects that may signal a fault
#
# Any effect that may signal a fault should inherit from fallible to permit
# its use within type parameteric features such as `eff.try`
#
public fallible(ERROR type, h ERROR->void) : effect is


  # cause fault with the given error
  #
  public cause(e ERROR) void
  => h e


  # type feature that must be implemented by all children to create an
  # instance of `fallible.this.type` with the given error handler.
  #
  public type.new(# the error handler, this will usually abort this effect or
                  # abort using another effect (e.g., panic),
                  #
                  h ERROR->void
                  ) fallible.this
  => abstract


  # try -- run code and handle fault of type `fallible.this`.
  #
  # try is a type feature that can be called on any child of `fallible`
  # to run the code given as an argument and handle an error using the
  # `Unary` provided to `catch`.
  #
  # This enables Java-like try-catch syntax as follows
  #
  #   res := FALLIBLE_TYPE.try ()->
  #             code
  #          .catch s->
  #             handle failure `s`
  #
  # or even
  #
  #   res := FALLIBLE_TYPE.try code || (s->handle failure `s`)
  #
  # Note that the code is not executed unless `.catch` or `infix ||` is applied
  # to the result of the call to `try`.
  #
  public type.try(T type, code_try ()->T) =>
    eff.try ERROR fallible.this T code_try
last changed: 2025-01-23