0.0.2 • Published 2 years ago

as-option v0.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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