RE: reference initialization

From: Hongwei Xi (hwxi@ECECS.UC.EDU)
Date: Fri May 12 2000 - 22:38:48 MET DST

  • Next message: Hongwei Xi: "Re: reference initialization"

    > 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

    Yes, in theory, it requires null check at every use. However,
    I assume that a marjority of such null checks can be readily
    eliminated using flow analysis, though things can become difficult
    when arrays are involved. Note that Java is imperative, which
    makes flow analysis easier (compared to ML).

    A common saying is that a program is made safer if references
    are always initialized. I find this is contrary to the truth.
    In this case, a program is made safer if you insert run-time
    checks (and rely on a compiler to remove redundant checks)

    If I use 'get' and 'set', is there a compiler to eliminate such
    checks (i.e., after 'set', 'get' should do no tag checking)?

    Yes, ML allows the programmer to tag values (using option
    types) and thus shifts the burden to the programmer. In Java,
    this is done automatically (and we can rely on a compiler to
    remove redundant null checks). Which is better?

    --Hongwei



    This archive was generated by hypermail 2b29 : Sun May 14 2000 - 23:18:35 MET DST