From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.4 required=5.0 tests=AWL,DNS_FROM_RFC_ABUSE autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 56E80BC82 for ; Mon, 13 Aug 2007 09:12:34 +0200 (CEST) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l7D7CWQQ003139 for ; Mon, 13 Aug 2007 09:12:33 +0200 Received: from localhost (orion [130.54.16.5]) by kurims.kurims.kyoto-u.ac.jp (8.13.7/8.13.7) with ESMTP id l7D0PYw9020676; Mon, 13 Aug 2007 09:25:34 +0900 (JST) Date: Mon, 13 Aug 2007 09:25:28 +0900 (JST) Message-Id: <20070813.092528.214236695.garrigue@math.nagoya-u.ac.jp> To: nanaki@gmail.com Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Order of evaluation when constructing record values From: Jacques Garrigue In-Reply-To: <6806cf750708111056v19107705p288e0b0bb152ef63@mail.gmail.com> References: <6806cf750708111056v19107705p288e0b0bb152ef63@mail.gmail.com> X-Mailer: Mew version 4.2 on Emacs 22.1 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Miltered: at discorde with ID 46C00460.001 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; stdin:01 ocaml:01 unspecified:01 iirc:01 datatype:01 forgiveness:98 ints:01 caml-list:01 int:01 int:01 garrigue:03 garrigue:03 jacques:03 jacques:03 let:03 From: "Jeff Meister" > I ask for forgiveness in advance for this silly pedantic question. > > I think my question is best illustrated with an example. Say I have > this simple record type for holding a date: > > type date = { year : int; month : int; day : int; } > > Now, I want to read the year/month/day values from stdin, and I know > they will appear in that order. So I do the following: > > let today = { > year = read_int (); > month = read_int (); > day = read_int (); > } > > For this to work, I need the ints to be read in the order given, or I > could end up with a day of 2007 and a year of 11. Is there any > guarantee that OCaml will follow that order of evaluation when > constructing the record? Or do I have to force it with let-bindings > like this: > > let today = > let y = read_int () in > let m = read_int () in > let d = read_int () in > { year = y; month = m; day = d; } > > Of course, it's not that big a deal for me to just use the let-binding > method, but I'm curious, and it might make my code look nicer if I can > rely on order of evaluation. You definitely have to use let-bindings. The order of evaluation without let-bindings is left unspecified in the reference manual. IIRC, in practice this should be right-to-left, following the definition of the datatype (not the order in the function!), but this does not seem a good idea to rely on that. Jacques Garrigue