caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Pattern-matching on Ocaml records
@ 2011-05-02 12:32 Create Software
  2011-05-02 12:44 ` Andreas Rossberg
  0 siblings, 1 reply; 5+ messages in thread
From: Create Software @ 2011-05-02 12:32 UTC (permalink / raw)
  To: caml-list

Hi all,

I'm building an old Caml Light program with OCaml, and I've come across a reluctant piece of code. Here's a a mwe:

   type t = {mutable label: int};;
   let reset {label = lbl} = lbl <- 0;;

   let test = {label = -1};;
   reset test;;
   test;;

This compiles fine in Caml Light, yielding

   Type t defined.
   reset : t -> unit = <fun>
   test : t = {label = -1}
   - : unit = ()
   - : t = {label = 0}

On the other hand, Ocaml refuses this snippet, and prints the following message:

   Characters 26-34:
     let reset {label = lbl} = lbl <- 0;;
                               ^^^^^^^^
   Error: Unbound instance variable lbl

Was this construct deprecated? If so, why?

Thanks!
Clément.


PS: I also tried

   let reset' a =
     let lbl = a.label in
     lbl <- 0
   ;;

but "The identifier lbl is not mutable.", in both OCaml and Caml Light. Why so?

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

* Re: [Caml-list] Pattern-matching on Ocaml records
  2011-05-02 12:32 [Caml-list] Pattern-matching on Ocaml records Create Software
@ 2011-05-02 12:44 ` Andreas Rossberg
  2011-05-02 13:16   ` Create Software
  0 siblings, 1 reply; 5+ messages in thread
From: Andreas Rossberg @ 2011-05-02 12:44 UTC (permalink / raw)
  To: Create Software; +Cc: caml-list

On May 2, 2011, at 14:32, Create Software wrote:
> PS: I also tried
>
>  let reset' a =
>    let lbl = a.label in
>    lbl <- 0
>  ;;
>
> but "The identifier lbl is not mutable.", in both OCaml and Caml  
> Light. Why so?

Try:

   a.lbl <- 0

Only the record field itself is mutable. Extracting only gives you a  
value, not a reference.

/Andreas


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

* Re: [Caml-list] Pattern-matching on Ocaml records
  2011-05-02 12:44 ` Andreas Rossberg
@ 2011-05-02 13:16   ` Create Software
  2011-05-02 13:24     ` Andreas Rossberg
  2011-05-02 13:33     ` Jacques Garrigue
  0 siblings, 2 replies; 5+ messages in thread
From: Create Software @ 2011-05-02 13:16 UTC (permalink / raw)
  To: Andreas Rossberg; +Cc: caml-list

> Only the record field itself is mutable. Extracting only gives you a value, not a reference.

Sure, but how come that
     let reset {label = lbl} = lbl <- 0;;
works fine in Caml Light and not in OCaml?

Clément.

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

* Re: [Caml-list] Pattern-matching on Ocaml records
  2011-05-02 13:16   ` Create Software
@ 2011-05-02 13:24     ` Andreas Rossberg
  2011-05-02 13:33     ` Jacques Garrigue
  1 sibling, 0 replies; 5+ messages in thread
From: Andreas Rossberg @ 2011-05-02 13:24 UTC (permalink / raw)
  To: Create Software; +Cc: caml-list

On May 2, 2011, at 15:16, Create Software wrote:
>> Only the record field itself is mutable. Extracting only gives you  
>> a value, not a reference.
>
> Sure, but how come that
>    let reset {label = lbl} = lbl <- 0;;
> works fine in Caml Light and not in OCaml?

Same reason: pattern matching extracts the value. I can't tell why it  
worked in Caml Light, though, since I'm not familiar enough with it.

/Andreas


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

* Re: [Caml-list] Pattern-matching on Ocaml records
  2011-05-02 13:16   ` Create Software
  2011-05-02 13:24     ` Andreas Rossberg
@ 2011-05-02 13:33     ` Jacques Garrigue
  1 sibling, 0 replies; 5+ messages in thread
From: Jacques Garrigue @ 2011-05-02 13:33 UTC (permalink / raw)
  To: Create Software; +Cc: caml-list users

On 2011/05/02, at 22:16, Create Software wrote:

>> Only the record field itself is mutable. Extracting only gives you a value, not a reference.
> 
> Sure, but how come that
>    let reset {label = lbl} = lbl <- 0;;
> works fine in Caml Light and not in OCaml?

This has been deprecated.
Namely, this introduced a notion of lvalue, only for mutable record labels (and actually also for sum types IIRC).
The current approach of always requiring to specify the record is clearer, and it extends naturally the notations
   a.(i) <- e  for arrays
   s.[i] <- e  for strings

This said, later on object instance variables were added as lvalues, so the uniformity argument is not so strong...

Jacques Garrigue

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

end of thread, other threads:[~2011-05-02 13:34 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-02 12:32 [Caml-list] Pattern-matching on Ocaml records Create Software
2011-05-02 12:44 ` Andreas Rossberg
2011-05-02 13:16   ` Create Software
2011-05-02 13:24     ` Andreas Rossberg
2011-05-02 13:33     ` Jacques Garrigue

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