Created
September 5, 2012 19:58
-
-
Save liamoc/3643591 to your computer and use it in GitHub Desktop.
Type-safe sum types in C
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
// needs -fnested-functions. Can be done without, but makes the whole thing a lot more cumbersome | |
// and less useful. | |
#include <stdio.h> | |
#define my_data_alts ( void (*A)(int) , void (*B)(int, int) , void (*C)(char, int) ) | |
#define make_my_data(n) void n my_data_alts | |
typedef void (*my_data) my_data_alts; | |
void show(my_data match) { | |
void A(int n) { printf("A %d", n); } | |
void B(int a, int b) { printf("B %d %d", a, b); } | |
void C(char a, int b) { printf("C %d %c", a, b); } | |
match(A,B,C); | |
} | |
int main() { | |
make_my_data (foo) { A(1); }; | |
show (foo); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment