try.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 try
#
# Author: Fridtjof Siebert (siebert@tokiwa.software)
#
# -----------------------------------------------------------------------
# try -- simple exception effect
#
# try provides an operation 'raise' that immediately stops execution and
# returns an 'error' wrapped in an 'outcome'.
#
public try (public T type) : effect effect_mode.plain
is
# mutuable field to receive the error in case raise is called
#
private err option error := nil
# install this effect and execute 'f'. Wrap the result of 'f' into an
# 'outcome' if 'f' returns normally, otherwise if 'f' is aborted early
# via a call to 'raise' wrap the 'error' passed to 'raise' into the
# resulting 'outcome'.
#
public on(R type, f ()->R) outcome R =>
run (outcome R) (()->outcome f()) ()->err.get
# terminate immediately with the given error wrapped in 'option'.
#
public raise(e error) =>
set err := e
abort
# convenience routine to create a new instance of 'try' and run 'f' in
# it.
#
public try(T, R type, f ()->R) =>
(try T).on f
# convenience routine to create a new instance of 'try' and run 'f' in
# it. Return the result of 'f' directly or panic in case 'f' calls
# 'try.env.raise'.
#
public try_or_panic(T, R type, f ()->R) outcome R =>
match (try T).on f
e error => panic $e
r R => r
last changed: 2024-03-07