|
Like NULL, confusion over EOF is a problem which can be eliminated via algebraic types. What if instead of a char, getchar() returned an Option<char>? Then you can pattern match, something like this Rust/C mashup: match getchar() {
Some(c) => putchar(c),
None => break,
}
Magical sentinels crammed into return values — like EOF returned by getchar() or -1 returned by ftell() or NULL returned by malloc() — are one of C's drawbacks. |
Every time I have to check for in-band errors in C, or pass a pointer to a function as a "return value", I think of this and cringe.