A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/jmi2k/disjoint.h below:

jmi2k/disjoint.h: Disjoint types (aka sum types) for C

disjoint - disjoint types (aka sum types) for C
#include <assert.h>
#include <stdio.h>
#include "disjoint.h"

disjoint Machine {
	variants(EMPTY, IDLE, BROKEN) {
		unsigned int bottles;
		char *error;
	};
};

#define Empty emptyvariant(disjoint Machine, EMPTY)
#define Idle(B) variant(disjoint Machine, IDLE, .bottles = B)
#define Broken(E) variant(disjoint Machine, BROKEN, .error = E)

void
takebottle(disjoint Machine *m)
{
	match (*m) {
	case EMPTY:
		printf("No bottles left.\n");
		break;
	case IDLE:
		printf("Take it! Bottles left: %u\n", --m->bottles);
		if (!m->bottles)
			*m = Empty;
		break;
	case BROKEN:
		printf("ERROR %p: %s\n", (void *) m, m->error);
		break;
	}
}

void
refill(disjoint Machine *m, unsigned int bottles)
{
	assert(variantof(*m) == EMPTY);
	*m = Idle(bottles);
}

int
main(void)
{
	disjoint Machine m = Idle(4);

	while (variantof(m) != EMPTY)
		takebottle(&m);

	return 0;
}
clib install jmi2k/disjoint.h

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4