Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Damien Doligez <Damien.Doligez@inria.fr>
To: caml-list@inria.fr
Subject: Re:  Is this use of Object.magic safe?
Date: Tue, 14 Nov 2000 15:41:32 +0100 (MET)	[thread overview]
Message-ID: <200011141441.PAA0000012997@beaune.inria.fr> (raw)

>From: Stephan Houben <stephan@pcrm.win.tue.nl>
>
>let value_of_int (i : int) : value =
>  Obj.magic i
>
>let value_of_allocated_value (av :allocated_value) : value=
>  Obj.magic av
> 
>let unwrap (int_func : int -> 'a) 
>                (aval_func : allocated_value -> 'a)
>                (v : value) : 'a =
>  if Obj.is_int (Obj.repr v)
>  then int_func (Obj.magic v)
>  else aval_func (Obj.magic v) 

>1. Is this safe w.r.t. the garbage collector? Or am I going to confuse the GC?

No problem with the GC.


>2. Can I safely marshall values?

No problem with marshalling either.


>3. Is it worth the trouble? Perhaps O'Caml already does something smart
>   when a constructor of the form `Int of int' is encountered?

It doesn't work as you expect because O'Caml is strongly typed, so it
can (and does) use a smart representation for your constant
constructors True and False  They are represented as integers, so your
unwrap function cannot tell the difference between a boolean and an
integer.  This is typical of the dangers of using Obj.magic.

-- Damien



             reply	other threads:[~2000-11-15 21:38 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-11-14 14:41 Damien Doligez [this message]
  -- strict thread matches above, loose matches on Subject: below --
2000-11-13  8:43 Stephan Houben
2000-11-14 21:31 ` Anton Moscal

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200011141441.PAA0000012997@beaune.inria.fr \
    --to=damien.doligez@inria.fr \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox