Skip to content

Instantly share code, notes, and snippets.

@saschagrunert
Last active March 30, 2018 13:09
Show Gist options
  • Save saschagrunert/ccddd1d405fe24dbbffe7d7f51172958 to your computer and use it in GitHub Desktop.
Save saschagrunert/ccddd1d405fe24dbbffe7d7f51172958 to your computer and use it in GitHub Desktop.
Either example with structs
#include "include/Func.h"
#include <stdio.h>
struct baz { int value; };
EITHER(struct baz *, baz, char, bax);
int main(void) {
struct baz value = { 3 };
Either(baz, bax) result = Left_baz_bax(&value);
if (isLeft(result)) {
printf("should be printed\n");
}
if (isRight(result)) {
printf("should not be printed\n");
}
// the alternative value
struct baz defaultValue = { 0 };
struct baz * newResult = fromLeft(&defaultValue, result);
printf("extracted result = %d\n", newResult->value);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment