Pointers in ALFE

This is part of the ALFE types series.

As a high-performance language with C++ heritage, it should not be at all surprising that ALFE has pointers.

Given a type Foo, the type of a pointer that points to values of that type is Foo*.

In C and C++, the grammar binds the asterisk to the variable name rather than the type. So (particularly in C) declarations of pointers look like this:

Foo *foo;

That was very confusing to me when I first started learning C. Once I thought of it as:

Foo* foo;

instead, the whole thing made much more sense. In C++, this style is idiomatic, due to C++'s emphasis on types over expressions. However, the grammar hasn't caught up so:

Foo* foo, bar;

declares a Foo* called foo and a Foo called bar.

ALFE fixes this once and for all:

Foo* foo;

declares a real Foo* called foo rather than an imaginary Foo called *foo.

The second difference between ALFE and C++ is that pointers are by default not nullable. In other words, Foo* carries the connotation of pointing to an actual Foo, so null checking is not required (or even possible, since comparing a Foo* to Null will always yield false). In other words, ALFE fixes Tony Hoare's billion dollar mistake. Put like that, it's a no-brainer.

Of course, in practice there will be such things as an uninitialized Foo*, one that points to a Foo that went out of scope or one that points to one past the end of an array. However, those aren't things that could be checked for by a function which just takes a Foo* as an argument - if the pointer might be invalid there would have to be an out of band signalling mechanism saying whether it is safe to dereference that pointer or not. I'll be talking about an ideally suited mechanism in a future post in this series.

Foo* is syntactic sugar for the template instantiation Pointer<Foo>. This is mostly to simplify the implementation of the compiler, but it does also mean that the type constructor Pointer could be passed to some template that has a parameter of kind <>. Not sure why you might want to do such a thing, but it's there is somebody does find a use for it.

Leave a Reply