Version française
Home     About     Download     Resources     Contact us    
Browse thread
Question about float refs.
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Till Varoquaux <till@p...>
Subject: Re: [Caml-list] Question about float refs.
What you want is the system to check at runtime whether it should box
the values or not. If you really care about this behaviour you can use
arrays instead of records:

type 'a ref = 'a array
let ref v = Array.create 1 v

let (!) v = v.(0)

let ( := ) v x = v.(0) <- x

I don't expect this to come without a runtime cost.
Till

On Thu, Aug 19, 2010 at 9:34 AM, David House <dmhouse@gmail.com> wrote:
> Note that you can directly observe boxedness using the Obj functions:
>
> # type t1 = { a : float; b : int };;
> type t1 = { a : float; b : int; }
> # type t2 = { c : float; d : float };;
> type t2 = { c : float; d : float; }
> # let x1 = { a = 1.; b = 2 };;
> val x1 : t1 = {a = 1.; b = 2}
> # let x2 = { c = 3.; d = 4. };;
> val x2 : t2 = {c = 3.; d = 4.}
> # Obj.tag (Obj.repr x1);;
> - : int = 0
> # Obj.tag (Obj.repr x2);;
> - : int = 254
>
> For the use case you describe, I believe your best bet is to define a
> float_ref type as you describe.
>
> _______________________________________________
> 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
>