Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Record pattern matching
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Don Syme <dsyme@m...>
Subject: [Caml-list] Record pattern matching

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 = <fun>

# function {b=b} -> b;;
- : x -> int = <fun>
#

I guess this is considered a feature, but I just wanted to report that
in my current situation I actually find it unhelpful.  I'm in the
process of adding fields to a large number of existing records in a
large existing code base, and would like the type checker to notify me
every time a pattern match is used against one of these record types -
essentially every place where I do such a pattern match I will have some
extra work to do and I'd rather the type checker guided me to these
locations.

If I had encoded the type as a datatype constructor, then the rules are
of course different:

# 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)

It seems you're faced with an unfortunate either-or: you lose some
strictness in your type checking (i.e. the type checker ends up catching
less bugs), or you lose the syntactic convenience of records.  

I don't really have a suggestion as to what to do about this, though I
guess I would prefer if pattern matching against records was strict, or
if an alternative syntax could be used for stricter record patterns.  As
an aside, I would also prefer it if you did not have to use the "{a=a;
b=b}" syntax but could write "{a; b}".

Thanks,
Don


-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr