On 09.02.2024 07:05, Lawrence D'Oliveiro wrote:
> Modula-2 had this interesting feature where, for example if you had a
> pointer variable whose pointed-to objects were of type T
>
> VAR
> ptr : POINTER TO T;
>
> and you had a statement like
>
> ptr := NEW(T);
>
> then the compiler would translate the “NEW(T)” into “ALLOCATE(TSIZE(T))”,
> [...]
The type bound pointer type was already introduced by Niklaus Wirth
in Pascal (with an only slightly different syntax, type PT = ^T ),
so it's not too surprising that he used that concept also in Modula.
>
> C could benefit from some similar high-level+low-level layering like this,
> don’t you think?
Yes. Type bound pointers have been said (I think by F. L. Bauer)
to be sort of a tamed/controlled version of an inherently insecure
(pointer-)concept. I'm not sure, though, (and too tired to ponder
about it) but I seem to recall that with that coupling there's the
possibility to write type-safe pointer constructs; that would have
(IIRC) to be supported by the compiler. So introducing it in C as a
layer on top would probably not suffice to gain the same safety.
Hiding only the allocation would obviously be just syntactic sugar.
You find that binding also in C++ (as has been mentioned elsethread)
but that had been borrowed from Simula: REF(T) p; p :- new T; Both
languages allow to assign subtypes in a class hierarchy, of course,
to make most sense.
Janis