Superfunc Maybe Save

An Exception-Free Optional Type for Nim

Project README

maybe

Build status badge

Nimble Package: https://nimble.directory/pkg/maybe

Docs: https://superfunc.github.io/maybe

Note 1: ~There is a chance the main macro(maybeCase) may get merged into the standard library. If this happens I'll recommend people use that, but will accept bugfixes and reports on this library going forward, just no new features.~

Note 2: The PR was not accepted, so maybe lives on!

An implementation of a maybe type, also known as option(al) in other languages.

Why Not Just use Option[T] from the standard library?: In short, this library doesn't throw exceptions. It achieves this by using a macro to provide a safe pattern in which a maybe object can't be invalidly accessed, see maybeCase in the docs for further details. For a small example:

var m = maybe.just(4)
maybeCase m:
  just x:
    var y = 3
    echo $(x+y)
  nothing:
    echo "no value"

var nada = maybe.nothing[int]()
maybeCase nada:
  just foo:
    echo "hi this is a value we cant print" & $foo
  nothing:
    echo "nope no value, nice try with your invalid access"
    
## This prints out:
## >> 7
## >> nope no value, nice try with your invalid access

Note that trying to access our local binding(x and foo) outside of the just blocks will result in a compile time error. This is how we achieve safe access.

Installation

Should be installed via nimble

nimble install maybe

License Info

Copyright (c) Josh Filstrup 2014-2019 Licensed under BSD3 (see license.md for details)

Open Source Agenda is not affiliated with "Superfunc Maybe" Project. README Source: superfunc/maybe
Stars
36
Open Issues
1
Last Commit
4 years ago
Repository
License
Tags

Open Source Agenda Badge

Open Source Agenda Rating