NULLs

Hans-Dieter.Dreier@materna.de Hans-Dieter.Dreier@materna.de
Fri, 16 Jul 1999 18:42:48 +0200


--GcO7w8ZkekqBxKaI7BOzgRxLaVM6tJFK
Content-type: text/plain; charset="ISO-8859-1"
Content-transfer-encoding: quoted-printable

>Hi.
>
>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 b=
e a "has-a" relationship rather "is-a".
Anyway, as long as no check has been made, the compiler will allow only ope=
rations of the the most derived common ancestor of both ref and null becaus=
e that is the best guess it can make.

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 that's the case, then any operation that accepts
>a ref as a parameter will expect that parameter not to be null.

Correct.

> If you pass in
>a pointer (which
>should be ok because pointer is a subtype of ref) it could possibly be nul=
l
>and your operation won't even bother to check for this because it thinks i=
t
>only has a ref.
>
>I think that you really want the pointer class to be a supertype of ref. T=
his
>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 becau=
se that's how it was declared. You could set it to null, for example. Keywo=
rd: Contravariance. The problem would have to be caught by the runtime (ove=
rriding the "set-to-null" functionality of pointer to produce an exception)=
, which is a Bad Thing for a statically type-safe system.

>Jason.
>
>
>


--

Regards,

Hans-Dieter Dreier
(Hans-Dieter.Dreier@materna.de)=

--GcO7w8ZkekqBxKaI7BOzgRxLaVM6tJFK
Content-type: text/plain; charset="ISO-8859-1"
Content-transfer-encoding: quoted-printable

IDENTIFIKATIONSANGABEN:
a24166a.txt IA5 DX-MAIL X.400 User Agent=

--GcO7w8ZkekqBxKaI7BOzgRxLaVM6tJFK--