[M3devel] A bizarre language quirk
Rodney M. Bates
rodney_bates at lcwb.coop
Wed Mar 21 19:22:53 CET 2018
Modula3, 2.6.3 (Designators), concerning subscripting says:
An expression of the form a[i_1, ..., i_n] is shorthand for a[i_1]...[i_n].
If a is a reference to an array, then a[i] is shorthand for a^[i].
These have the unfortunate (and maybe unintended?) interaction that
a[i_1][i_2] could subscript a 2-d array, or it could mean a[i_1]^[i_2],
if a is an array of pointers to arrays. The compiler handles either,
as determined by the type of a.
But the compiler discards the distinction between a[i_1,i_2] and
a[i_1][i_2] at parse time, converting the former to the the latter.
This in turn means it will also interpret a[i_1,i_2] as a[i_1]^[i_2]
if the type of a calls for it.
As written, the language allows this. A rewrite using each of the
language shorthand rules in the order given does it. But this seems
utterly bizarre, and a horrible case of syntactic misleadingness.
Anybody aware of existing code that would be undermined by making
the compiler refuse the latter interpretation?
I have long been very skeptical about the wisdom of allowing the
implied dereference, even with only one subscript. But discovering
this leaves me with no longer any doubt that it is a bad idea. The
only benefit is saving a single keystroke for the writer, who has the
context well in mind already and only does this once. The cost is
considerably greater difficulty to readers' & maintainers'
understanding what it means. They often don't have the context
without searching, possibly widely, in the code. And this is liable
to happen several times.
rodney.m.bates at acm.org
More information about the M3devel