Skip to content

a less fascist fork of nim-result

License

Notifications You must be signed in to change notification settings

disruptek/badresults

Repository files navigation

badresults

Test Matrix GitHub release (latest by date) Minimum supported Nim version License

see 2.1.x releases if you need support for an earlier Nim compiler

This is mostly a copy of an early version of the Status nim-result package that differs in that you do not need to provide a side-effect-free (func) implementation of $ for all types used in your Result.

Also, it makes fewer uses of templates and has a narrower API to make it simpler and prevent C-codegen issues experienced with nim-result.

Also, it supports toException and $ overrides for error types which are implemented in nested scopes.

Also, it might not work in Windows because I have no way to test it there.

If you care more for performance and features, use nim-result or ask nicely and maybe we add a symbol or two back in. I personally consider compilation a non-negotiable feature.

Documentation

See the documentation for the badresults module as generated directly from the source.

Here's the type we provide:

Result[T; E] = object
  case o: bool
  of false:
      e: E
  of true:
      v: T

Usage

This example is from the documentation:

# It's convenient to create an alias - most likely, you'll do just fine
# with strings as error!

type R = Result[int, string]

# Once you have a type, use `ok` and `err`:

func works(): R =
  # ok says it went... ok!
  R.ok 42
func fails(): R =
  # or type it like this, to not repeat the type!
  result.err "bad luck"

# If you provide this exception converter, this exception will be raised
# on dereference
func toException(v: Error): ref CatchableException = (ref CatchableException)(msg: $v)
try:
  get RE[int].err(a)
except CatchableException:
  echo "in here!"

Installation

$ nimph clone badresults

or if you're still using Nimble like it's 2012,

$ nimble install https://github.com/disruptek/badresults

License

MIT