optional 0.1.0

An optional/maybe type with range semantics


To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:

Optional type for D

Latest version Build Status codecov license

Full API docs available here

Represents an optional data type that may or may not contain a value. Matches behavior of haskell maybe and scala or swift optional type.

E.g.

import optional;

// Create empty optional
auto a = no!int;

// Try doing stuff, all results in none
assert(a == none);

++a; // none;
a - 1; // none;

// Assign and try doing the same stuff
a = 9;
assert(a == some(9));

++a; // some(10);
a - 1; // some(9);

// Acts like a range as well

import std.algorithm: map;
import std.conv: to;

auto b = some(10);
auto c = no!int;

b.map!(to!double) // [10.0]
c.map!(to!double) // empty

// Can safely dispatch to whatever inner type is
struct A {
    struct Inner {
        int g() { return 7; }
    }
    Inner inner() { return Inner(); }
    int f() { return 4; }
}

auto d = some(A());

// Dispatch to one of its methods

d.dispatch.f(); // calls a.f, returns some(4)
d.dispatch.inner.g(); // calls a.inner.g, returns some(7)

auto e = no!(A*); 

// If there's no value in the optional or it's a pointer that is null, dispatching still works, but produces none
assert(e.dispatch.f() == none);
assert(e.dispatch.inner.g() == none);

// Set a value and now it will work
e = new A;

assert(e.dispatch.f() == some(4));
assert(e.dispatch.inner.g() == some(7));

Authors:
  • Ali Akhtarzada
Dependencies:
bolts
Versions:
1.3.0 2021-Oct-12
1.2.1 2021-Apr-08
1.2.0 2021-Jan-14
1.2.0-beta.1 2020-Dec-30
1.1.0 2020-Oct-14
Show all 45 versions
Download Stats:
  • 9 downloads today

  • 44 downloads this week

  • 116 downloads this month

  • 13896 downloads total

Score:
3.1
Short URL:
optional.dub.pm