Skip to content

Instantly share code, notes, and snippets.

@jahan-addison
Last active February 20, 2021 23:37
Show Gist options
  • Save jahan-addison/33c9ad38a1ae7e5fe171b81ec4fbf539 to your computer and use it in GitHub Desktop.
Save jahan-addison/33c9ad38a1ae7e5fe171b81ec4fbf539 to your computer and use it in GitHub Desktop.
// License: Public domain
// see: https://creativecommons.org/publicdomain/zero/1.0/deed.en
interface Option<T> {
value: T | null;
}
/**
* First-class None subtype for a value that is Maybe.None.
*/
export class None<T> implements Option<null> {
public value = null
}
/**
* First class Some<T> subtype for a value that is Maybe<T>Some(value).
*/
export class Some<T> implements Option<T> {
public value: T;
public constructor(value: T) {
this.value = value;
}
}
/**
* Factory wrapper of Some, None, so that pattern matching is possible.
*/
export default class Maybe {
public static Some<T>(value: T): Some<T> {
return new Some<T>(value);
}
public static None(): Maybe {
return new None() as Maybe;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment