as-option

0.0.2 • Public • Published

NPM registryNPM license

Option

Work In Progress

Zero cost abstraction for optional container (Option<T>). This abstraction is completely free for reference types, while other types that cannot yet be nullable such as bool, i32 or f64 will be wrapped in the class.

Usage

import { Option, Some, None } from "as-option/assembly"

function maybeArray(arr: i32[] | null): Option<i32[]> {
  if (arr != null) {
    return Some(arr);
  } else {
    return None<i32[]>();
  }
}

function getDefaultFloat(input: Option<f64>): f64 {
  return input.unwrapOr(0.0);
}

function getDefaultInt(input: Option<i32>): i32 {
  return input.unwrapOrElse(() => -1);
}

TODO

  • [ ] Support zero cost for types with sizeof<T>() <= 16
  • [ ] Tests

Package Sidebar

Install

npm i as-option

Weekly Downloads

462

Version

0.0.2

License

MIT

Unpacked Size

8.83 kB

Total Files

14

Last publish

Collaborators

  • maxgraey