From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id VAA15416; Sun, 13 May 2001 21:58:03 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id VAA15376 for caml-list@pauillac.inria.fr; Sun, 13 May 2001 21:58:02 +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 UAA30691 for ; Mon, 7 May 2001 20:37:55 +0200 (MET DST) Received: from localhost.localdomain (ppp215.dyn142.pacific.net.au [210.23.142.215]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f47Ibof13804 for ; Mon, 7 May 2001 20:37:51 +0200 (MET DST) Received: from ozemail.com.au (IDENT:root@localhost [127.0.0.1]) by localhost.localdomain (8.9.3/8.8.7) with ESMTP id EAA26546; Tue, 8 May 2001 04:37:43 +1000 Message-ID: <3AF6EB77.E5473778@ozemail.com.au> Date: Tue, 08 May 2001 04:37:43 +1000 From: John Max Skaller X-Mailer: Mozilla 4.7 [en] (X11; I; Linux 2.2.12-20 i686) X-Accept-Language: en MIME-Version: 1.0 To: Don Syme CC: caml-list@inria.fr Subject: Re: [Caml-list] Record pattern matching References: <0C682B70CE37BC4EADED9D375809768A48FDDC@red-msg-04.redmond.corp.microsoft.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Don Syme wrote: > > In OCaml record patterns may be inexact, i.e. you do not have to specify > all the fields. > > # type x = {a:int; b:int};; > type x = { a : int; b : int; } > > # function {a=a} -> a;; > - : x -> int = > > # function {b=b} -> b;; > - : x -> int = > # > > I guess this is considered a feature, but I just wanted to report that > in my current situation I actually find it unhelpful. You'd rather be forced to code something like: function { a=a; b=_ } -> a;; where all the fields have to be named, but some of them can be specified as ignored? > # type x = Foo of int * int;; > type x = Foo of int * int > > # function (Foo (x)) -> x > Characters 10-17: > The constructor Foo expects 2 argument(s), > but is here applied to 1 argument(s) Actually .. I just got bitten by a 'counterexample' to this assertion :-) Consider the following: type t = Ctor int * int match Ctor (x,_) -> fst x Now, I change it: type t = Ctor int * (int * int) match Ctor (x,_) -> fst x This code still compiles, it still type checks, and it still operates: but it returns the wrong value entirely. This problem would go away if I were forced to model the entire structure: match Ctor (x,(_,_)) but there is no easy way to say where the level of detail should stop. There is a sense in which record.a is just a shorthand for match record with { a=value } -> value which means that you might argue that the notation record.a should be completed by naming every field too :-) What you might do is change the field names in the record: this will certainly give an error on every record access (but that might be more than you want :-) however, that suggests a pragmatic technique -- although it would have to be 'retrofitted': to each record, add a dummy field: type t = { bank_account_v_1: unit; a : int; .... } and add _that_ to all your matches: function { bank_account_v_1=dummy; a = a } -> a When you extend or change the record, modify that field: type t = { bank_account_v_2 : unit; ... } and the match above will now fail, while field access with '.' will continue to work. -- John (Max) Skaller, mailto:skaller@maxtal.com.au 10/1 Toxteth Rd Glebe NSW 2037 Australia voice: 61-2-9660-0850 checkout Vyper http://Vyper.sourceforge.net download Interscript http://Interscript.sourceforge.net ------------------- To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr