as-option
Version:
Zero cost Option type for AssemblyScript
36 lines (25 loc) • 959 B
Markdown
[](https://www.npmjs.com/package/as-option)[](LICENSE.md)
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
```ts
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