From 9940ef69251c68c9261e00d299cd922ec34ab9f4 Mon Sep 17 00:00:00 2001 From: Tim Whiting Date: Fri, 10 Jan 2025 22:57:45 -0700 Subject: [PATCH] Use better file location for unjust, and add expect to provide an explicit error message. --- lib/std/core/maybe.kk | 10 ++++++++-- lib/std/core/maybe2.kk | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/std/core/maybe.kk b/lib/std/core/maybe.kk index f96c329ae..801e553c6 100644 --- a/lib/std/core/maybe.kk +++ b/lib/std/core/maybe.kk @@ -26,10 +26,16 @@ pub fun default( m : maybe, nothing : a ) : a Just(x) -> x // Get the value of the `Just` constructor or raise an exception -pub fun unjust( m : maybe ) : exn a +pub fun unjust( m : maybe, ?kk-file-line: string) : exn a match m Just(x) -> x - Nothing -> throw("unexpected Nothing in std/core/maybe/unjust") + Nothing -> throw("unexpected Nothing in " ++ ?kk-file-line) + +// Get the value of the `Just` constructor or raise an exception with `error-msg` +pub fun expect( m : maybe, error-msg: string) : exn a + match m + Just(x) -> x + Nothing -> throw(error-msg) pub fun map( m : maybe, f : a -> e b ) : e maybe match m diff --git a/lib/std/core/maybe2.kk b/lib/std/core/maybe2.kk index 10c7ff0a5..f7278a23b 100644 --- a/lib/std/core/maybe2.kk +++ b/lib/std/core/maybe2.kk @@ -26,10 +26,16 @@ pub fun default( m : maybe2, nothing : (a,b) ) : (a,b) Just2(x,y) -> (x,y) // Get the value of the `Just` constructor or raise an exception -pub fun unjust2( m : maybe2 ) : exn (a,b) +pub fun unjust( m : maybe2, ?kk-file-line: string) : exn (a,b) match m Just2(x,y) -> (x,y) - Nothing2 -> throw("unexpected Nothing2 in std/core/maybe/unjust2") + Nothing2 -> throw("unexpected Nothing2 in " ++ ?kk-file-line) + +// Get the value of the `Just` constructor or raise an exception +pub fun expect( m : maybe2, error-msg: string) : exn (a,b) + match m + Just2(x,y) -> (x,y) + Nothing2 -> throw(error-msg) pub fun map( m : maybe2, f : (a,b) -> e (c,d) ) : e maybe2 match m