Skip to content

Commit

Permalink
Merge pull request #2941 from quantified-uncertainty/throw
Browse files Browse the repository at this point in the history
Common.throw() Function
  • Loading branch information
OAGr committed Jan 10, 2024
2 parents 6b4a506 + 19bc13c commit 56b203e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/smooth-fireants-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@quri/squiggle-lang": patch
---

Added Common.throw
11 changes: 11 additions & 0 deletions packages/squiggle-lang/src/errors/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,14 @@ export class REAmbiguous extends ErrorMessage {
return `Ambiguous Error: ${this.message}`;
}
}

// Used for user-created throw() function calls
export class REThrow extends ErrorMessage {
constructor(public msg: string) {
super(msg);
}

toString() {
return `${this.message}`;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { REThrow } from "../errors/messages.js";
import { makeDefinition } from "../library/registry/fnDefinition.js";
import {
frAny,
Expand Down Expand Up @@ -55,4 +56,22 @@ export const library = [
),
],
}),
maker.make({
name: "throw",
description:
"Throws a fatal error. There is no way in the language to catch this error.",
definitions: [
makeDefinition(
[frOptional(frNamed("message", frString))],
frAny(),
([value]) => {
if (value) {
throw new REThrow(value);
} else {
throw new REThrow("Common.throw() was called");
}
}
),
],
}),
];
4 changes: 2 additions & 2 deletions packages/squiggle-lang/src/library/registry/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { library as booleanLibrary } from "../../fr/boolean.js";
import { library as builtinLibrary } from "../../fr/builtin.js";
import { library as commonLibrary } from "../../fr/common.js";
import { library as calculatorLibrary } from "../../fr/calculator.js";
import { library as dangerLibrary } from "../../fr/danger.js";
import { library as dateLibrary } from "../../fr/date.js";
Expand Down Expand Up @@ -54,7 +54,7 @@ const fnList: FRFunction[] = [
...unitsLibrary,
...calculatorLibrary,
...inputLibrary,
...builtinLibrary, // should go last, because has some catch-all functions
...commonLibrary, // should go last, because has some catch-all functions
];

export const registry = Registry.make(fnList);
Expand Down

4 comments on commit 56b203e

@vercel
Copy link

@vercel vercel bot commented on 56b203e Jan 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

quri-ui – ./packages/ui

quri-ui.vercel.app
quri-ui-quantified-uncertainty.vercel.app
quri-ui-git-main-quantified-uncertainty.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 56b203e Jan 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 56b203e Jan 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 56b203e Jan 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.