Obj.magic

From: David Chemouil (David.Chemouil@enseeiht.fr)
Date: Fri Jun 09 2000 - 10:57:28 MET DST

  • Next message: David Chemouil: "polymorphic variants (2) + magic"

    Hi again,

    I'd like to get information about Obj.magic. I know it is some kind of
    identity function `a -> `b, but I'd like to know how it can be used, why
    it can be interesting and why, also, it is sometimes dangerous
    (considering some older posts on this mailing-list). For example, I
    found myself trying some different things (with polymorphic variants and
    coercions, too):

    # type arg = [`A | `B];;
    type arg = [ `A | `B]
    # let id : arg -> arg = function
      | `A -> `A
      | `B -> `B;;
    val id : arg -> arg = <fun>
    # let x = `A;;
    val x : [> `A] = `A
    # id x;;
    - : arg = `A
    # x;;
    - : [> `A] = `A
    # let y = ref `A;;
    val y : _[> `A] ref = {contents=`A}
    # id !y;;
    - : arg = `A
    # y;;
    - : arg ref = {contents=`A}
    # let z = Obj.magic `A;;
    val z : '_a = <poly>
    # id z;;
    - : arg = `A
    # z;;
    - : arg = `A
    # let t = (x :> arg);;
    val t : arg = `A

    -- 
    David Chemouil [mailto:chemouil@enseeiht.fr] [mobile: 06 84 16 26 65]
    

    Laboratoire d'informatique et de mathématiques appliquées (IRIT-INPT)

    "Je vous ai fait trop faibles pour sortir du gouffre, parce que je vous ai fait assez forts pour n'y point tomber" -- Rousseau



    This archive was generated by hypermail 2b29 : Fri Jun 09 2000 - 19:46:28 MET DST