Skip to content

Commit f22c578

Browse files
committed
Add "attempt" keyword for go-like error handling
Returns a pair (value, err) If code evaluates successfully, then err is null. Otherwise, it's an Error object with information about the error that occurred.
1 parent e87e4b5 commit f22c578

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

M2/Macaulay2/d/binding.d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ bumpPrecedence();
318318
export breakpointS := special("breakpoint", unaryop, precSpace, wide);
319319
export profileS := special("profile", unaryop, precSpace, wide);
320320
export shieldS := special("shield", unaryop, precSpace, wide);
321+
export attemptS := special("attempt", unaryop, precSpace, wide);
321322
export throwS := special("throw", nunaryop, precSpace, wide);
322323
export returnS := special("return", nunaryop, precSpace, wide);
323324
export breakS := special("break", nunaryop, precSpace, wide);

M2/Macaulay2/d/evaluate.d

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,6 +1677,16 @@ export toExpr(err:Error):Expr := (
16771677
storeInHashTable(h, toExpr("message"), toExpr(err.message));
16781678
Expr(sethash(h, false)));
16791679

1680+
attemptfun(c:Code):Expr := (
1681+
r := tryEval(c);
1682+
if tryEvalSuccess then seq(r, nullE)
1683+
else (
1684+
when r
1685+
is err:Error do seq(nullE, toExpr(err))
1686+
-- shouldn't happen since tryEvalSuccess only false when r is an Error
1687+
else buildErrorPacket("unable to catch error")));
1688+
setupop(attemptS, attemptfun);
1689+
16801690
assigntofun(lhs:Code,rhs:Code):Expr := (
16811691
left := eval(lhs);
16821692
when left is Error do return left else (

M2/Macaulay2/m2/exports.m2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ export {
514514
"atan",
515515
"atan2",
516516
"atanh",
517+
"attempt",
517518
"autoload",
518519
"baseFilename",
519520
"baseName",

0 commit comments

Comments
 (0)