Created
September 26, 2020 07:33
-
-
Save dckc/9e04e4604498eab72410a487b6d28b8d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
new out, Result, REVAddress(`rho:rchain:REVAddress`) in { | |
/** | |
* The RChain genesis contracts typically use | |
* (true, T) \/ (false, Problem) to indicate | |
* a success or failure outcome. | |
* We abbreviate this as Result<T> (cf. rust). | |
* | |
* And we use `Problem` for { "message": String, ... } | |
*/ | |
/** | |
* We represent delimited continuations using channels. | |
* In the JS prototype, an Ejector never returns. | |
* @typedef { (reason: any) => never } Ejector | |
*/ | |
/** | |
* Escape takes a thunk that expresses failure | |
* by which channel it sends on and turns it | |
* (back) into expressing failure using a value, | |
* i.e. `(true, value) \/ (false, problem)`. | |
* | |
* JSDoc from prototype: | |
* @param {(ej: Ejector) => Promise<T>} thunk | |
* @returns { Promise<Result<T>> } | |
* @template T | |
*/ | |
contract Result(@"escape", thunk, return) = { | |
new ej, r2 in { | |
thunk!(*ej, *r2) | | |
for (@problem <- ej) { | |
return!((false, problem)) | |
} | | |
for (@value <- r2) { | |
return!((true, value)) | |
} | |
} | |
} | |
| | |
/** | |
* Use `expect` to put the successful outcome on | |
* the return channel or eject the problem. | |
* | |
* @param { (p: any) => never } ej | |
* @param { Result<T> } result | |
* @returns { T } | |
* @template T | |
*/ | |
contract Result(@"expect", ej, @[ok /\ Bool, vp], return) = { | |
match ok { | |
true => return!(vp) | |
false => ej!(vp) | |
} | |
} | |
| | |
// testing | |
new resultCh, ej, ret in { | |
REVAddress!("fromPublicKey", "junk", *resultCh) | | |
Result!("expect", *out, *resultCh, *ret) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment