Created
December 1, 2015 13:44
-
-
Save roobie/9c2c9de14022a59d51bb to your computer and use it in GitHub Desktop.
a port of the `Result` type in Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const Result = function Result() {}; | |
const Err = function Err(value) { | |
this.value = value; | |
}; | |
const Ok = function Ok(value) { | |
this.value = value; | |
} | |
Result.match = function match(result, matcher, dynamicThis) { | |
return (matcher[result.constructor.name] || function () {}) | |
.call(dynamicThis, (result || {}).value); | |
}; | |
Result.err = function err(value) { | |
return new Err(value); | |
}; | |
Result.ok = function ok(value) { | |
return new Ok(value); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment