From 1f259480b417a944837dcdc2eb59696692495833 Mon Sep 17 00:00:00 2001 From: Jon Pretty Date: Wed, 10 Jul 2024 13:38:07 +0200 Subject: [PATCH] Add amalgamation --- src/core/contingency-core.scala | 8 +++++- src/core/contingency.AmalgamateTactic.scala | 31 +++++++++++++++++++++ src/test/contingency.Tests.scala | 12 +++++++- 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 src/core/contingency.AmalgamateTactic.scala diff --git a/src/core/contingency-core.scala b/src/core/contingency-core.scala index 3d1e9c5..25e3d43 100644 --- a/src/core/contingency-core.scala +++ b/src/core/contingency-core.scala @@ -103,7 +103,13 @@ def attempt[ErrorType <: Exception](using DummyImplicit)[SuccessType] : Attempt[SuccessType, ErrorType] = boundary: label ?=> - Attempt.Success(block(using AttemptTactic[ErrorType, SuccessType](label))) + Attempt.Success(block(using AttemptTactic(label))) + +def amalgamate[ErrorType <: Exception](using DummyImplicit)[SuccessType] + (block: AmalgamateTactic[ErrorType, SuccessType] ?=> SuccessType) + : SuccessType | ErrorType = + boundary: label ?=> + block(using AmalgamateTactic(label)) def abandonment[ErrorType <: Error](using Quotes, Realm)[SuccessType] (block: AbandonTactic[ErrorType, SuccessType] ?=> SuccessType) diff --git a/src/core/contingency.AmalgamateTactic.scala b/src/core/contingency.AmalgamateTactic.scala new file mode 100644 index 0000000..8ee0842 --- /dev/null +++ b/src/core/contingency.AmalgamateTactic.scala @@ -0,0 +1,31 @@ +/* + Contingency, version [unreleased]. Copyright 2024 Jon Pretty, Propensive OÜ. + + The primary distribution site is: https://propensive.com/ + + Licensed under the Apache License, Version 2.0 (the "License"); you may not use this + file except in compliance with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software distributed under the + License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + either express or implied. See the License for the specific language governing permissions + and limitations under the License. +*/ + +package contingency + +import language.experimental.pureFunctions + +import rudiments.* + +@capability +class AmalgamateTactic[ErrorType <: Exception, SuccessType] + (label: boundary.Label[SuccessType | ErrorType]) +extends Tactic[ErrorType]: + type Result = SuccessType | ErrorType + type Return = SuccessType | ErrorType + + def record(error: ErrorType): Unit = boundary.break(error)(using label) + def abort(error: ErrorType): Nothing = boundary.break(error)(using label) diff --git a/src/test/contingency.Tests.scala b/src/test/contingency.Tests.scala index 97f68bc..5f5cc91 100644 --- a/src/test/contingency.Tests.scala +++ b/src/test/contingency.Tests.scala @@ -125,4 +125,14 @@ object Tests extends Suite(t"Contingency tests"): abort(BetaError()) "success" - .assert(_ == "beta") + test(t"amalgamation failure"): + amalgamate: + abort(BetaError()) + 42 + .assert(_ == BetaError()) + + test(t"amalgamation success"): + amalgamate: + if false then abort(BetaError()) + 42 + .assert(_ == 42)