Skip to content

potential package: assert #3

@aslilac

Description

@aslilac

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions