type-checking brainstorm (was: [cvs] bogofilter/src datastore_db.c, 1.185, 1.186)

Matthias Andree matthias.andree at gmx.de
Wed Mar 9 03:48:40 CET 2005


Look at this sample code, it will trigger warnings when passing
mismatched directory or file name types. I'm not sure if this can be
written even shorter (perhaps only in C99) as it causes quite some
typing unfortunately. Type safeness is nice when you have it,
but C isn't Ada.

Here's the code:

---try.c----------------------------------------------------------
#include <stdio.h>

struct dirname {
    const char *dirname;
};

typedef struct dirname dirname;

struct filename {
    const char *filename;
};

typedef struct filename filename;

void pd(dirname *d) { printf("%s", d->dirname); }
void pf(filename *f) { printf("%s", f->filename); }

void test(void) {
    filename n = { "filename" };
    dirname d = { "dirname" };

    pd(&n); /* bad */
    pf(&n); /* good */
    pd(&d); /* good */
    pf(&d); /* bad */
}
---EOF------------------------------------------------------------

-- 
Matthias Andree
_______________________________________________
Bogofilter-dev mailing list
Bogofilter-dev at bogofilter.org
http://www.bogofilter.org/mailman/listinfo/bogofilter-dev



More information about the bogofilter-dev mailing list