-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
Implementation would probably be mostly two functions
assert_or_die
pub fn assert_or_die(val: Bool) {
case val {
True -> val
False -> panic
}
}Use case
pub fn process_data(important_data: Data) {
assert_or_die(isValidSignature(important_data.signature))
// do things with data
}This is useful, especially for hacking things out quickly, but isn't very idiomatic, hence the need for our second implementation.
assert_or_error
pub fn assert_or_error(val: Bool, error: e, func: fn() -> a) -> Result(a, e) {
case val {
True -> Ok(func())
False -> Error(error)
}
}Use case
pub type DataProcessingError {
InvalidSignature
}
pub fn process_data(important_data: Data) -> Result(a, DataProcessingError) {
use <- assert_or_error(isValidSignature(important_data.signature), InvalidSignature)
// do things with data
}This still allows us to make assumptions in our code, but allows us to propagate errors instead of killing the program outright.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels