From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id XAA04955 for caml-redist@pauillac.inria.fr; Sun, 14 May 2000 23:13:14 +0200 (MET DST) Resent-Message-Id: <200005142113.XAA04955@pauillac.inria.fr> Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id WAA19635 for ; Fri, 12 May 2000 22:39:14 +0200 (MET DST) Received: from babbage.ececs.uc.edu (mail.ececs.uc.edu [129.137.8.2]) by nez-perce.inria.fr (8.10.0/8.10.0) with ESMTP id e4CKd3b06676 for ; Fri, 12 May 2000 22:39:08 +0200 (MET DST) Received: from gatekeeper-internal.ececs.uc.edu (gatekeeper-internal.ececs.uc.edu [129.137.8.10]) by babbage.ececs.uc.edu (8.9.3+Sun/8.9.3) with ESMTP id QAA25688; Fri, 12 May 2000 16:38:48 -0400 (EDT) Date: Fri, 12 May 2000 16:38:48 -0400 (EDT) From: Hongwei Xi To: Stephanie Weirich cc: "'caml-list@inria.fr'" Subject: RE: reference initialization In-Reply-To: Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Resent-From: weis@pauillac.inria.fr Resent-Date: Sun, 14 May 2000 23:13:14 +0200 Resent-To: caml-redist@pauillac.inria.fr > 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