From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: weis Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id OAA06661 for caml-redist; Thu, 11 May 2000 14:54:41 +0200 (MET DST) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id GAA29383 for ; Wed, 10 May 2000 06:50:25 +0200 (MET DST) Received: from babbage.ececs.uc.edu (mail.ececs.uc.edu [129.137.8.2]) by concorde.inria.fr (8.8.7/8.8.7) with ESMTP id GAA00864 for ; Wed, 10 May 2000 06:50:20 +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 AAA03254 for ; Wed, 10 May 2000 00:50:10 -0400 (EDT) Date: Wed, 10 May 2000 00:50:10 -0400 (EDT) From: Hongwei Xi To: caml-list@inria.fr Subject: reference initialization In-Reply-To: <200004251816.UAA13437@pauillac.inria.fr> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: weis > Wrong. You have references, which are quite better than pointers > (they are typed, and necessarily initialized) I have given some thoughts on this one. I find that it is really not a good design choice to initialize every reference in all ML-like languages (yes, I well realize the difficulty in not doing so). Here is my argument. The common wisdom is that if we initialize every reference immediately after it is created then we shall never read from an uninitialized reference. But this is a lame argument. Suppose I use a reference 'x'. If I know what the initial value of 'x' should be, I'll of course prefer to initialize it with that value. Now suppose I don't, that is, I intend to assign a value to 'v' later (maybe in a loop or in a conditional branch) (1) ML strategy: initialize x with any value 'v' of an appropriate type (sometimes, such a 'v' is difficult to find or takes time to construct (consider 'x' to be a large array)). Now suppose I make a mistake, forgetting to assign a value to 'x' later. What happens is that the computation now happily use the *wrong* initial value and sings the song "well-typed-program-can- never-go-wrong" until a (most likely) wrong answer or exception is returned. (2) Java Strategy: let us say we have the same scenario as before but 'x' is not initialized. If I read from x before assigning a value to 'x', the execution abnormally stops (yes, we need run-time checking). I think this is much better than to carry the execution further until a wrong answer or exception is returned. 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) --Hongwei Xi \~~~~/ \\ // \\ // @ Mail: hwxi@ececs.uc.edu C-o^o, ))__|| \\__//_ // \\ Url: http://www.ececs.uc.edu/~hwxi ( ^ ) ))__|| \--/-\\ \\ / \V\ )) || // \\ \\ Tel: +1 513 556 4762 (office) ------ // || o // \\ \\//Fax: +1 513 556 7326 (department)