caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Record field update using 'with' syntax
@ 2006-09-18 20:22 Denis Bueno
  2006-09-18 20:58 ` [Caml-list] " Martin Jambon
  0 siblings, 1 reply; 4+ messages in thread
From: Denis Bueno @ 2006-09-18 20:22 UTC (permalink / raw)
  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
| #
`----


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Record field update using 'with' syntax
  2006-09-18 20:22 Record field update using 'with' syntax Denis Bueno
@ 2006-09-18 20:58 ` Martin Jambon
  2006-09-18 21:00   ` Denis Bueno
  0 siblings, 1 reply; 4+ messages in thread
From: Martin Jambon @ 2006-09-18 20:58 UTC (permalink / raw)
  To: Denis Bueno; +Cc: OCaml Mailing List

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Record field update using 'with' syntax
  2006-09-18 20:58 ` [Caml-list] " Martin Jambon
@ 2006-09-18 21:00   ` Denis Bueno
  2006-09-18 21:16     ` Yoann Padioleau
  0 siblings, 1 reply; 4+ messages in thread
From: Denis Bueno @ 2006-09-18 21:00 UTC (permalink / raw)
  To: Martin Jambon; +Cc: OCaml Mailing List

On 9/18/06, Martin Jambon <martin1977@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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Record field update using 'with' syntax
  2006-09-18 21:00   ` Denis Bueno
@ 2006-09-18 21:16     ` Yoann Padioleau
  0 siblings, 0 replies; 4+ messages in thread
From: Yoann Padioleau @ 2006-09-18 21:16 UTC (permalink / raw)
  To: Denis Bueno; +Cc: Martin Jambon, OCaml Mailing List

"Denis Bueno" <dbueno@gmail.com> writes:

> On 9/18/06, Martin Jambon <martin1977@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 ? 


>>
>
> Makes sense. Thanks.
>
> -Denis
>
> _______________________________________________
> 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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2006-09-18 21:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-09-18 20:22 Record field update using 'with' syntax Denis Bueno
2006-09-18 20:58 ` [Caml-list] " Martin Jambon
2006-09-18 21:00   ` Denis Bueno
2006-09-18 21:16     ` Yoann Padioleau

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).