nicoletta triolo | 18 Nov 16:39

Re: Obj.magic

Thanks very much.
I'm studying OcamlP3L system and in its source there is a large use of Obj.magic, so I wanted to know something deeper about it.

Bye,
nicoletta
--- Mar 18/11/08, Dario Teixeira <darioteixeira <at> yahoo.com> ha scritto:
Da: Dario Teixeira <darioteixeira <at> yahoo.com>
Oggetto: Re: "ocaml_beginners"::[] Obj.magic
A: ocaml_beginners <at> yahoogroups.com
Data: Martedì 18 novembre 2008, 16:25

Hi,

> I can't find exhaustive documentation about this

> function. Could somebody explain me its specification?

The first rule of Obj.magic is that you do not talk about

Obj.magic... On a more serious note, Obj.magic is used to

"magically" overrule the type system, allowing you to do

a low-level cast of a type into another. As an example,

try this code in the top-level:

let foo : float = Obj.magic 1;;

Note that this is not a safe type-casting. In fact, on my

machine the above example causes a segfault.

While gurus can certainly find situations where Obj.magic is

indeed useful, I reckon it's almost certain that if you think

you might need Obj.magic, then there must be something wrong

with your design or you are trying to do things which are not

possible within the Ocaml type system.

Cheers,

Dario Teixeira











Unisciti alla community di Io fotografo e video, il nuovo corso di fotografia di Gazzetta dello sport:
http://www.flickr.com/groups/iofotografoevideo

[Non-text portions of this message have been removed]

__._,_.___
Recent Activity
Visit Your Group
Search Ads

Get new customers.

List your web site

in Yahoo! Search.

Yahoo! Finance

It's Now Personal

Guides, news,

advice & more.

Special K Group

on Yahoo! Groups

Join the challenge

and lose weight.

.

__,_._,___
Christophe TROESTLER | 18 Nov 18:53
X-Face
Favicon

Re: Obj.magic

On Tue, 18 Nov 2008 15:39:52 +0000 (GMT), nicoletta triolo wrote:
>
> Thanks very much.
> I'm studying OcamlP3L system and in its source there is a large use of Obj.magic, so I wanted to know something deeper about it.
>

external magic : 'a -> 'b = "%identity"

Like its definition says: it is the identity function, it does
nothing. As Dario told you already, it is just used to circumvent the
type system. To understand why it is needed, you need to know the
type of [e] and of [Obj.magic e] and why one has to force the
conversion of one to the other. To make sure it will not generate a
segfault, you need (at least) to understand the representation of
OCaml values (chapter 18 of the manual:
http://caml.inria.fr/pub/docs/manual-ocaml/manual032.html).

Hope it helps,
C.

__._,_.___
Recent Activity
Visit Your Group
Yahoo! Finance

It's Now Personal

Guides, news,

advice & more.

New business?

Get new customers.

List your web site

in Yahoo! Search.

Yahoo! Groups

Going Green Zone

Find ways to go green.

Join a green group.

.

__,_._,___

Gmane