NULLs

jason jason@injektilo.org
Fri, 16 Jul 1999 19:15:42 -0700



Hans-Dieter.Dreier@materna.de wrote:

> > You would define a parameterized class from the predefined parameterized
> >class "ref" that does not allow NULL pointers:
> >>
> >> class pointer <a> : ref <a> | null;
> >>
> >> pointer <int> ptr1;
> >> ref <int> ptr2;
> >>
> >> ptr1 allows NULLs, ptr2 doesn't.
> >>
> >
> >This could be a problem. I'm assuming that you're declaring that pointer
> >derives from ref, correct?
>
> No. It *may* be a ref (genuine ref, not derived from ref), but it may also be a NULL. Or contain a null? I'm not sure. Thinking it over, it seems to be a "has-a" relationship rather "is-a".
> Anyway, as long as no check has been made, the compiler will allow only operations of the the most derived common ancestor of both ref and null because that is the best guess it can make.
>

So in this case it wouldn't allow any operations until the check was performed. Unless of course, NULL was a derivative of Object (or Any or whatever your base class for all classes in the language might be named).

>
> If the right hand side of the definition contains a "|" (meaning "...or may be a..." in this case), there is no  direct inheritance involved. Maybe it would have been better to pick another syntax altogether.
>

If it can lead to confusion, and it obviously did in this case, it should be changed. I do realize these are simply examples and not any definitive syntax.

How about:

class pointer <a>  :? ref <a> | null

It looks funny but you definitely won't be confusing it with inheritance.

> >I think that you really want the pointer class to be a supertype of ref. This
> >would prevent you passing a pointer to an operation that required a ref.
> >Sather has this concept although I've only read about it so couldn't tell you
> >how well it works in
> >practice.
>
> If pointer were a supertype of ref, you could pass a ref to a function that expects a pointer.
> Inside the function, the compiler would treat the ref to be a pointer because that's how it was declared. You could set it to null, for example. Keyword: Contravariance. The problem would have to be caught by the runtime (overriding the "set-to-null" functionality of pointer to produce an exception), which is a Bad Thing for a statically type-safe system.
>

Good point. Now that I understand your idea more, it makes a lot of sense. It seems to fit nicely into a static type system as long as the compiler requires you to test the type.

Jason.