Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Caml-list] Record field update using 'with' syntax

17 views
Skip to first unread message

Denis Bueno

unread,
Sep 18, 2006, 4:27:22 PM9/18/06
to OCaml Mailing List
Suppose a module M1 with the record

type foo = {x : int; y : int; z : int};;

and a member of its type. In another module, a qualified field
matching like the following works:

match instance with
{M1.x = 5; y = 5; z = 100} -> (* something *)

Why doesn't the following work in a function?

{M1.instance with x = 20}

It would seem easy to determine that x must name a field of a record inside M1.

Thanks for your help. (Example in OCaml 3.09.2 below.)

-Denis

Example:

,----
| Objective Caml version 3.09.2
|
| # module M1 = struct
| type foo = {x : int; y : int; z : int};;
| let foo = {x = 10; y = 10; z = 10};;
| end;;
| module M1 : sig type foo = { x : int; y : int; z : int; } val foo : foo end
| # match M1.foo with | {M1.x = 5; y = 5} -> ();;
| Characters 0-43:
| Warning P: this pattern-matching is not exhaustive.
| Here is an example of a value that is not matched:
| {x=0}
| match M1.foo with | {M1.x = 5; y = 5} -> ();;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| Exception: Match_failure ("", 1, 0).
| # {M1.foo with x = 5};;
| Characters 0-19:
| {M1.foo with x = 5};;
| ^^^^^^^^^^^^^^^^^^^
| Unbound record field label x
| #
`----

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Martin Jambon

unread,
Sep 18, 2006, 5:03:29 PM9/18/06
to Denis Bueno
On Mon, 18 Sep 2006, Denis Bueno wrote:

> Suppose a module M1 with the record
>
> type foo = {x : int; y : int; z : int};;
>
> and a member of its type. In another module, a qualified field
> matching like the following works:
>
> match instance with
> {M1.x = 5; y = 5; z = 100} -> (* something *)
>
> Why doesn't the following work in a function?
>
> {M1.instance with x = 20}

^^^^^^^^^^^
this is an arbitrary expression, it doesn't have to come from the
module where the record type is defined, so you need to specify it
too.

> Example:

> | # {M1.foo with x = 5};;
> | Characters 0-19:
> | {M1.foo with x = 5};;
> | ^^^^^^^^^^^^^^^^^^^
> | Unbound record field label x

That works:

# {M1.foo with M1.x = 0; y = 0};;
- : M1.foo = {M1.x = 0; M1.y = 0; M1.z = 10}


Martin

--
Martin Jambon, PhD
http://martin.jambon.free.fr

Denis Bueno

unread,
Sep 18, 2006, 5:04:04 PM9/18/06
to Martin Jambon
On 9/18/06, Martin Jambon <marti...@laposte.net> wrote:
> On Mon, 18 Sep 2006, Denis Bueno wrote:
> > Why doesn't the following work in a function?
> >
> > {M1.instance with x = 20}
> ^^^^^^^^^^^
> this is an arbitrary expression, it doesn't have to come from the
> module where the record type is defined, so you need to specify it
> too.
>

Makes sense. Thanks.

-Denis

Yoann Padioleau

unread,
Sep 18, 2006, 5:22:37 PM9/18/06
to Denis Bueno
"Denis Bueno" <dbu...@gmail.com> writes:

> On 9/18/06, Martin Jambon <marti...@laposte.net> wrote:
>> On Mon, 18 Sep 2006, Denis Bueno wrote:
>> > Why doesn't the following work in a function?
>> >
>> > {M1.instance with x = 20}
>> ^^^^^^^^^^^
>> this is an arbitrary expression, it doesn't have to come from the
>> module where the record type is defined, so you need to specify it
>> too.

Still, the machine could infer this information. Do we not all
like type inference ? Why not extend it to this problem ?

0 new messages