From bb16b6893390e2d9cf9f4d0474de5fe05c03c805 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Thu, 18 Apr 2024 18:43:10 +0200 Subject: [PATCH 1/6] add optional message to Option.getExn --- CHANGELOG.md | 1 + src/Core__Option.res | 10 ++++++++-- src/Core__Option.resi | 5 +++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 42a591bd..72a0a079 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - BREAKING: Use new native `bigint` type. This requires ReScript compiler version "11.1.0-rc.6" or higher. https://github.com/rescript-association/rescript-core/pull/207 - `Int`, `Float`, `BigInt`: use optional args and deprecate `xxxWithRadix`, `xxxWithPrecision` etc. https://github.com/rescript-association/rescript-core/pull/209 +- Add optional `~message: string=?` to `Option.getExn`. ## 1.2.0 diff --git a/src/Core__Option.res b/src/Core__Option.res index a799a0ea..63eeb7ff 100644 --- a/src/Core__Option.res +++ b/src/Core__Option.res @@ -34,10 +34,16 @@ let forEach = (opt, f) => | None => () } -let getExn = x => +let getExn = (x, ~message=?) => switch x { | Some(x) => x - | None => raise(Not_found) + | None => + Core__Error.panic( + switch message { + | None => "Not found." + | Some(message) => message + }, + ) } external getUnsafe: option<'a> => 'a = "%identity" diff --git a/src/Core__Option.resi b/src/Core__Option.resi index fbaceba4..6f920896 100644 --- a/src/Core__Option.resi +++ b/src/Core__Option.resi @@ -66,18 +66,19 @@ Option.forEach(None, x => Console.log(x)) // returns () let forEach: (option<'a>, 'a => unit) => unit /** -`getExn(opt)` returns `value` if `opt` is `Some(value)`, otherwise raises an exception. +`getExn(opt, ~message=?)` returns `value` if `opt` is `Some(value)`, otherwise raises an exception with the message provided, or a generic message if no message was provided. ```rescript Option.getExn(Some(3)) // 3 Option.getExn(None) /* Raises an Error */ +Option.getExn(None, ~message="was None!") /* Raises an Error with the message "was None!" */ ``` ## Exceptions - Raises an error if `opt` is `None` */ -let getExn: option<'a> => 'a +let getExn: (option<'a>, ~message: string=?) => 'a /** `getUnsafe(opt)` returns `value` if `opt` is `Some(value)`, otherwise `undefined`. From ccd58aad80ba28d800755919ff3c6298738fb6ea Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Thu, 18 Apr 2024 18:44:36 +0200 Subject: [PATCH 2/6] changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72a0a079..5d7412a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ - BREAKING: Use new native `bigint` type. This requires ReScript compiler version "11.1.0-rc.6" or higher. https://github.com/rescript-association/rescript-core/pull/207 - `Int`, `Float`, `BigInt`: use optional args and deprecate `xxxWithRadix`, `xxxWithPrecision` etc. https://github.com/rescript-association/rescript-core/pull/209 -- Add optional `~message: string=?` to `Option.getExn`. +- Add optional `~message: string=?` to `Option.getExn`. https://github.com/rescript-association/rescript-core/pull/212 ## 1.2.0 From 2d63daf4109cd789b1d35f71c12825a8dcb88a4f Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Thu, 18 Apr 2024 18:46:17 +0200 Subject: [PATCH 3/6] commit regenerated JS code --- src/Core__Option.mjs | 9 ++++----- test/Test.mjs | 2 +- test/TypedArrayTests.mjs | 10 +++++----- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/Core__Option.mjs b/src/Core__Option.mjs index 776bc0bf..fb6c2e4d 100644 --- a/src/Core__Option.mjs +++ b/src/Core__Option.mjs @@ -1,6 +1,7 @@ // Generated by ReScript, PLEASE EDIT WITH CARE import * as Caml_option from "rescript/lib/es6/caml_option.js"; +import * as Core__Error from "./Core__Error.mjs"; function filter(opt, p) { if (opt !== undefined && p(Caml_option.valFromOption(opt))) { @@ -16,14 +17,12 @@ function forEach(opt, f) { } -function getExn(x) { +function getExn(x, message) { if (x !== undefined) { return Caml_option.valFromOption(x); + } else { + return Core__Error.panic(message !== undefined ? message : "Not found."); } - throw { - RE_EXN_ID: "Not_found", - Error: new Error() - }; } function mapOr(opt, $$default, f) { diff --git a/test/Test.mjs b/test/Test.mjs index 1fe28b2a..1824f710 100644 --- a/test/Test.mjs +++ b/test/Test.mjs @@ -13,7 +13,7 @@ function print(value) { if (match === "object" || match === "bigint") { return Util.inspect(value); } else if (match === "string") { - return Core__Option.getExn(JSON.stringify(value)); + return Core__Option.getExn(JSON.stringify(value), undefined); } else { return String(value); } diff --git a/test/TypedArrayTests.mjs b/test/TypedArrayTests.mjs index f18efc85..95a7391c 100644 --- a/test/TypedArrayTests.mjs +++ b/test/TypedArrayTests.mjs @@ -56,31 +56,31 @@ assertTrue("fromArray", (function () { return areSame(Core__Option.getExn(new BigInt64Array([ num1, num2 - ])[1]), num2); + ])[1], undefined), num2); })); assertTrue("fromBuffer", (function () { var x = new BigInt64Array(new ArrayBuffer(16)); x[1] = num2; - return areSame(Core__Option.getExn(x[1]), num2); + return areSame(Core__Option.getExn(x[1], undefined), num2); })); assertWillThrow("fromBuffer when too short can throw when used", (function () { var x = new BigInt64Array(new ArrayBuffer(1)); x[0] = num1; - areSame(Core__Option.getExn(x[0]), num1); + areSame(Core__Option.getExn(x[0], undefined), num1); })); assertTrue("fromBufferWithRange", (function () { var x = new BigInt64Array(new ArrayBuffer(16), 0, 1); x[0] = num1; - return areSame(Core__Option.getExn(x[0]), num1); + return areSame(Core__Option.getExn(x[0], undefined), num1); })); assertWillThrow("fromBufferWithRange is unsafe, out of range", (function () { var x = new BigInt64Array(new ArrayBuffer(16), 13, 1); x[0] = num1; - areSame(Core__Option.getExn(x[0]), num1); + areSame(Core__Option.getExn(x[0], undefined), num1); })); assertTrue("fromLength is NOT in bytes", (function () { From b82a00161f657b058aad38f8f302ef6f69282e84 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Fri, 19 Apr 2024 10:14:17 +0200 Subject: [PATCH 4/6] change error text --- src/Core__Option.res | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core__Option.res b/src/Core__Option.res index 63eeb7ff..ed55f10b 100644 --- a/src/Core__Option.res +++ b/src/Core__Option.res @@ -40,7 +40,7 @@ let getExn = (x, ~message=?) => | None => Core__Error.panic( switch message { - | None => "Not found." + | None => "Option.getExn called for None value" | Some(message) => message }, ) From 6ef70a11a7e229de32f20d3a5b81560e17e7dcae Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Fri, 19 Apr 2024 10:14:54 +0200 Subject: [PATCH 5/6] note in changelog as breaking --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d7412a6..21a3f4b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ - BREAKING: Use new native `bigint` type. This requires ReScript compiler version "11.1.0-rc.6" or higher. https://github.com/rescript-association/rescript-core/pull/207 - `Int`, `Float`, `BigInt`: use optional args and deprecate `xxxWithRadix`, `xxxWithPrecision` etc. https://github.com/rescript-association/rescript-core/pull/209 -- Add optional `~message: string=?` to `Option.getExn`. https://github.com/rescript-association/rescript-core/pull/212 +- BREAKING: Add optional `~message: string=?` to `Option.getExn`. This also changes the error raised by `Option.getExn` from `Not_found` to a regular JS error. https://github.com/rescript-association/rescript-core/pull/212 ## 1.2.0 From 61c90ede1148fce6da5e58762534768c4e96f020 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Fri, 19 Apr 2024 10:16:41 +0200 Subject: [PATCH 6/6] commit genrated files --- src/Core__Option.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core__Option.mjs b/src/Core__Option.mjs index fb6c2e4d..f0a8e401 100644 --- a/src/Core__Option.mjs +++ b/src/Core__Option.mjs @@ -21,7 +21,7 @@ function getExn(x, message) { if (x !== undefined) { return Caml_option.valFromOption(x); } else { - return Core__Error.panic(message !== undefined ? message : "Not found."); + return Core__Error.panic(message !== undefined ? message : "Option.getExn called for None value"); } }