Wandering Thoughts archives


My view of what 'strongly typed' means

I was recently reading this slide from a presentation, which set me to thinking about the whole issue of what makes me consider something to be 'strongly typed'.

In high-level languages, I have a pragmatic definition: if 2 + "3" succeeds, especially if it is 5, your language is weakly typed. Thus, awk and Perl are weakly typed but Python and Ruby are strongly typed.

(I have to restrict this to high-level languages because this works in C, although it does not give you 5. If you are lucky this particular case gives you a core dump.)

More generally, I tend to think of strong typing as having two attributes: type conversions among core types must be explicit, and they are not guaranteed to be possible (they can fail for some types or some values). Things like 2 + 3.5 are excused because I tend to lump all forms of numbers together in my mind as one big abstract Numbers type; it's acceptable for the language to implicitly convert back and forth among the subtypes.

(I have to restrict this to 'core types' because languages generally give user-written types some hooks to do automatic conversions, so in a nominally strongly typed language you can create what are effectively weakly typed types, things that will automatically 'convert' themselves to other types without any warnings.)

By contrast, weakly typed languages either perform implicit conversions or allow you to ultimately force a conversion to happen, and sometimes both. Perl does the former; C does the latter.

I suspect that this implies that strongly typed languages need either static typing or an exception system, since they must either prevent invalid conversions from ever being possible or have some way to signal a runtime error when one is attempted.

programming/StronglyTypedView written at 23:49:17; Add Comment

Page tools: See As Normal.
Login: Password:
Atom Syndication: Recent Pages, Recent Comments.

This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.