Created
September 9, 2017 03:00
-
-
Save chadaustin/4def9614c3c01d05693407cb934223cd to your computer and use it in GitHub Desktop.
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
#include <stdio.h> | |
#include <typeinfo> | |
#include <functional> | |
#include <string> | |
#include <type_traits> | |
#include <vector> | |
struct Unit {}; | |
template<typename Result, typename Error> | |
struct Promise { | |
template<typename AcceptCB> | |
auto then(const AcceptCB& accept) -> Promise<typename std::result_of<AcceptCB(Result)>::type, Error> { | |
acceptCBs.emplace_back(accept); | |
return Promise<typename std::result_of<AcceptCB(Result)>::type, Error>(); | |
} | |
template<typename AcceptCB, typename RejectCB> | |
auto then(const AcceptCB& accept, const RejectCB& reject) -> | |
Promise< | |
typename std::result_of<AcceptCB(Result)>::type, | |
typename std::result_of<RejectCB(Error)>::type | |
> | |
{ | |
acceptCBs.emplace_back(accept); | |
rejectCBs.emplace_back(reject); | |
return Promise< | |
typename std::result_of<AcceptCB(Result)>::type, | |
typename std::result_of<RejectCB(Error)>::type | |
>(); | |
} | |
std::vector<std::function<void(Result)>> acceptCBs; | |
std::vector<std::function<void(Error)>> rejectCBs; | |
// optional<either<Result, Error>> | |
bool resolved = false; | |
bool success = false; | |
Result result; | |
Error error; | |
}; | |
int main() { | |
Promise<int, std::string> p; | |
auto pr = p.then( | |
[](int) { | |
return 15.0; | |
} | |
).then( | |
[](double) { | |
return std::string("what"); | |
}, | |
[](const std::string&) { | |
return 5; | |
} | |
); | |
printf("%s\n", typeid(pr).name()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment