RE: reference initialization

From: Stephanie Weirich (sweirich@cs.cornell.edu)
Date: Thu May 11 2000 - 16:28:33 MET DST

  • Next message: John Prevost: "Re: reference initialization"

    > -----Original Message-----
    > From: Hongwei Xi [mailto:hwxi@ececs.uc.edu]
    > Sent: Wednesday, May 10, 2000 12:50 AM
    > To: caml-list@inria.fr
    > Subject: reference initialization
    >
    >
    > > Wrong. You have references, which are quite better than pointers
    > > (they are typed, and necessarily initialized)
    >
    > I have given some thoughts on this one.
    >
    [... parts elided ...]
    >
    > Can you still say that the ML strategy is better than the Java
    > strategy? I thus argue that it is better using dynamic checking
    > to detect reading from uninitialized reference than simply
    > assigning a value to every reference upon its creation.
    >
    > To summarize, my bottom line question is: what is really achieved
    > by assigning a reference a (wrong) initial value? Isn't this just
    > like an ostrich solving its problem by burying its head in sand?
    >
    > Of course, another problem with the ML strategy is efficiency loss
    > (which, though, is often negligible as discussed here before)

    The real difference between ML references and Java pointers is that ML
    separates "reference" from "possibly unitialized", while Java combines the
    two into one construct. It's not to difficult to implement Java-style
    pointers in ML, just use an option ref. For example:

    type 'a ptr = a' option ref
    exception NullPointer
    let new () = ref None
    let get x = match !x with Some y -> y | None -> raise NullPointer
    let set x y = x := Some y

    ML, of course, lacks the syntactic support to use these pointers as
    gracefully as Java can. On the other hand, the problem with _Java_ is
    efficiency loss, as the programmer cannot syntactically enforce that the
    reference is initialized -- requiring a null check at every use.

    -Stephanie



    This archive was generated by hypermail 2b29 : Thu May 11 2000 - 19:54:43 MET DST