caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* safe casting
@ 2007-08-22 18:33 Yitzhak Mandelbaum
  2007-08-22 22:20 ` [Caml-list] " David Allsopp
  0 siblings, 1 reply; 2+ messages in thread
From: Yitzhak Mandelbaum @ 2007-08-22 18:33 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 728 bytes --]

Hi,

Given two record types that are identical but in name, is it safe (if  
perhaps a poor idea) to cast between functions that are parametric in  
these record types:

e.g. Given,

module A =
struct
   type t = {a:int}
end

module B =
struct
   type t = {a:int}
end

Is this safe ?

module F(Ty: sig type t = {a:int} end) =
struct
   let extract {Ty.a=x} = x
end

module F_A = F(A)
let y = {B.a=3}
(Obj.magic F_A.extract) y

Put another way:  is the implementation of two records w/ identical  
labels the same, or is there a runtime significance to their static  
difference?

Thanks,
Yitzhak

--------------------------------------------------
Yitzhak Mandelbaum
AT&T Labs - Research

http://www.research.att.com/~yitzhak



[-- Attachment #2: Type: text/html, Size: 5859 bytes --]

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

* RE: [Caml-list] safe casting
  2007-08-22 18:33 safe casting Yitzhak Mandelbaum
@ 2007-08-22 22:20 ` David Allsopp
  0 siblings, 0 replies; 2+ messages in thread
From: David Allsopp @ 2007-08-22 22:20 UTC (permalink / raw)
  To: caml-list

> Given two record types that are identical but in name, is it safe (if
> perhaps a poor idea) to cast between functions that are parametric in
> these record types:

Yes, it's safe because the representations of the data are the same -
Obj.magic essentially removes all the protection of the type system by
giving you an 'a -> 'b function. And, yes, it's a bad idea unless you have a
*really* good reason to do it and a lot of time to comment your code ;o)

...

> Put another way: is the implementation of two records w/ identical labels
> the same, or is there a runtime significance to their static difference?

The labels of a record are only of concern to the type checker - they are
discarded at runtime. Two records will have the same runtime representation
iff the types of each member (reading left -> right) are the same.

e.g. type t = {a : int; b : int}
and  type u = {c : int; d : int}

are the same at runtime. I *think* you can also get away with:

type t = {a : int; b : int}
type u = {c : int; d : int; e : int}

(Obj.magic {c = 3; d = 4; e = 5} : t)

i.e. a "downcast" of a variable of type u to one of type t: but I'm not sure
that would "work" in all contexts (especially where copying may be
concerned). 

However, it's all terribly bad programming practice in O'Caml because you
end up with code that is totally resistant to change and that relies on the
Obj module (which is not officially supported). Chapter 18 (Interfacing with
C) of the manual helps with understanding the underlying representations of
values at runtime.

HTH,


David


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

end of thread, other threads:[~2007-08-22 22:20 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-08-22 18:33 safe casting Yitzhak Mandelbaum
2007-08-22 22:20 ` [Caml-list] " David Allsopp

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