Skip to content

Commit

Permalink
Add amalgamation
Browse files Browse the repository at this point in the history
  • Loading branch information
propensive committed Jul 10, 2024
1 parent 7d44612 commit 1f25948
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/core/contingency-core.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions src/core/contingency.AmalgamateTactic.scala
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 11 additions & 1 deletion src/test/contingency.Tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 1f25948

Please sign in to comment.