A while ago, I made a simple mistake using zlib:
char *file = "/some/path";
gzFile f = gzopen(file, "wb");
if (f) {
gzprintf(file, "something\n");
/* ... */
}
This is a static type error, of course: gzprintf
expects a gzFile
, not a char*
, and they have different C types. Unfortunately, C doesn't detect it, because (in older versions of zlib) gzFile
happens to be a typedef
for void*
, and in C char*
, like all pointers, can autoconvert to void*
. By accident of memory layout, this didn't crash at runtime either; it simply did nothing, causing me some puzzlement when this one line was missing from the otherwise intact output.
In addition to providing a puzzle, this mistake sheds light on some confusing terminology. Like most terms related to type, “weak typing” has several meanings. I usually reserve it for languages with typechecks that don't always apply, either because they can be suppressed by casts, or because some common1 operations don't provide them. But it's also used for the unrelated concept2 of autoconversion.
I thought this was just an arbitrary reuse of a term, but the autoconversion of char*
to gzFile
demonstrates a common meaning: autoconversion, like casts and missing type checks, means some type errors aren't detected. They usually cause conversions instead of blind misinterpretation of data (though not in this case), but from the user's point of view, the effect is the same: the type checks are unreliable. If you accidentally do arithmetic on a Perl string and it autoconverts to zero, it's small comfort that the conversion is type-safe — the program still continues happily computing incorrect results after a type error. So it makes sense to use the same term for any system of type checks that overlooks some errors. It's confusing to call completely different mechanisms by the same name, but the result is the same: both autoconversion and casts make type checks weak.
1 If some rare, exotic operations circumvent typechecks, that's OK, because you won't use them by accident.
2 And sometimes for even more unrelated concepts like dynamic typing, but this usage is usually either careless or pejorative. When people want to talk about dynamic typing, they don't call it “weak typing”.
No comments:
Post a Comment
It's OK to comment on old posts.