Proposal: use assert
#5290
acandoo
started this conversation in
Ideas & suggestions
Replies: 2 comments 4 replies
-
|
Hello! Thank you for your proposal. What problem in current Gleam code would be fixed by adding this feature? |
Beta Was this translation helpful? Give feedback.
1 reply
-
|
While it could avoid a bit of typing I feel like adding a new construct for panicking in the language is not all that needed given you could already use use result <- result.try(fallible_operation())
let assert Ok(wibble) = resultI don't think the extra typing is enough of a problem in itself to justify adding |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi! I've been playing with Gleam and have admired how intentionally scoped the language's syntax feels, and how it encourages simple, legible patterns. However, among other features, I feel like this one would fit in well with Gleam's existing semantics.
Proposal
Allow
assertions along withusesyntax. I know that assertions are generally not encouraged in library/application code/etc, but extending assertions would allow for more concise syntax that is imo still reasonably readable.Note that these examples are fabricated, since I'm unaware of a use with a current library.
Example 1: Single-argument assert
The base case for single-argument callbacks is simple enough.
Equivalent to:
Example 2: Single-argument assert with
asThis differs from the usual semantics of the
let assertsyntax in theasbeing on the left side, but I think it's necessary given the complexity of multi-argumentuse.Equivalent to:
Example 3: Multi-argument assert
I'm split on whether or not the
assertshould occur for every individual argument. I'm leaning towards having it for each argument for more explicitness:Equivalent to:
Though having one assert would make code more concise / closer to
let assert+ tuple destructuring:Example 4: Multi-argument assert with
asThis is, unfortunately, why
aswould have to be on the left side, unlike the usuallet assert.Equivalent to:
Beta Was this translation helpful? Give feedback.
All reactions