JSPM

as-option

0.0.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 137
  • Score
    100M100P100Q66001F
  • License MIT

Zero cost Option type for AssemblyScript

Package Exports

  • as-option

Readme

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