Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

potential package: assert #3

Open
aslilac opened this issue Mar 29, 2023 · 0 comments
Open

potential package: assert #3

aslilac opened this issue Mar 29, 2023 · 0 comments

Comments

@aslilac
Copy link
Contributor

aslilac commented Mar 29, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant