Version française
Home     About     Download     Resources     Contact us    
Browse thread
null values and sentinels
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jerry Jackson <jrj@c...>
Subject: null values and sentinels
Hello,

I'd like to know what the recommended solution to the following 
situation is...

I'd like to create a type like:

# type foo = {xxx: int; mutable yyy: foo};;

so that I can add nodes by mutating the "yyy" slots.  Unfortunately,
it seems that I must create a sentinel value of type "foo" to stand
for a null pointer.  This works but I'd like to be able to use pattern
matching on the value of yyy so as to get compile-time warnings if I
forget to check for null.

So, I tried this:

# type foo2 = {xxx: int; mutable yyy: bar} and bar = Empty | Pointer of
foo2;;

This works (i.e.  I can match on yyy in the way that I want), but
it _seems_ like this will create an extra indirection object in the
yyy slot.  Whether it does or not seems to be a compiler question to
me...  "Pointer of foo2" could be collapsed into foo2 by the
compiler.  Is it?  How should I approach this situation?

Thanks for any help,

Jerry Jackson