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
// And for the caller, the usage changes from this... | |
try { | |
float x = sqrt(-1); | |
std::cout << "sqrt(x) = " << x << std::endl; | |
} catch(std::string x) { | |
std::cout << "error occurred: " << x << std::endl; | |
} | |
// ... to this... | |
std::string msg = sqrt(-1) |
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
// This... | |
float sqrt(float x) { | |
if (x < 0) { | |
throw std::string("x should be >= 0"); | |
} | |
return computeSqrt(x); | |
} | |
// ... can be transformed into this... | |
Either<std::string, float> sqrt(float x) { |
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
Either<string, int> myEither = left("hello"); // constructs a either containing a leftValue; | |
int count = myEither | |
.rightMap([](auto num) { return num + 1; }) // adds 1 if rightValue is present | |
.leftMap([](auto str) { return str + "world"; }) // appends "world" if leftValue is present | |
.leftMap([](auto str) { return str.size(); }) | |
.join(); // both sides have now the same type, lets join... |
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
template<class Left, class Right> | |
struct Either { | |
union { | |
Left leftValue; | |
Right rightValue; | |
}; | |
bool isLeft; | |
}; |
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
int result = compute1(input); | |
int result2 = compute2(result); | |
if (has_errors()) { | |
std::cout << pop_error_message() << std::endl; | |
} |
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
const int ERROR = 1; | |
const int SUCCESS = 0; | |
int compute(int input, int* output) { | |
if (cond(input)) { | |
return ERROR; | |
} else { | |
*output = computeOutput(input); | |
return SUCCESS; | |
} |
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
int foo() throw(int) { | |
throw "bar"; | |
return 1; | |
} | |
int bar() noexcept { | |
throw 42; | |
return 1; | |
} |
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
int compute(int x) noexcept { | |
return x; | |
} | |
int compute(int x) throw(std::exception) { | |
return x; | |
} |
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
#include <iostream> | |
#include <string> | |
int divide(int x, int y) { | |
if (y == 0) { | |
throw std::string("Divide by zero"); | |
} | |
return x / y; | |
} |
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
cxx_test( | |
name = 'add', | |
srcs = [ | |
'add.cpp', | |
], | |
deps = [ | |
'//mathutils:mathutils', | |
], | |
) |