title: Is this use of Obj.magic necessary?
body: <p>I am reading a repository and I encountered this function in the body of some Yojson json parsing code:</p>
  <pre><code>let load_problems channel =
    let open Yojson.Basic.Util in
    let j = Yojson.Basic.from_channel channel in
    ...
    let rec unpack x =
      try magical (x |&gt; to_int) with _ -&gt;
      try magical (x |&gt; to_float) with _ -&gt;
      try magical (x |&gt; to_bool) with _ -&gt;
      try
        let v = x |&gt; to_string in
        if String.length v = 1 then magical v.[0] else magical v
      with _ -&gt;
      try
        x |&gt; to_list |&gt; List.map ~f:unpack |&gt; magical
      with _ -&gt; raise (Failure &quot;could not unpack&quot;)
    in
    ...
  </code></pre>
  <p>where <code>magical = Obj.magic</code>.  I understand what Obj.magic is (it's the equivalent to <code>Unsafe.Coerce</code> in Haskell), but I don't see why a type coercion is necessary here.  The <code>Yojson.Basic.Util</code> functions the author uses should already either succeed or fail to do this conversion.  Any intuition?</p>
  <p>EDIT:</p>
  <p>I feel I was depriving @glennsl of context, so here is the immediately following passage in which <code>unpack</code> is used:</p>
  <pre><code>  let tf = j |&gt; member &quot;tasks&quot; |&gt; to_list |&gt; List.map ~f:(fun j -&gt; 
        let e = j |&gt; member &quot;examples&quot; |&gt; to_list in
        let task_type = j |&gt; member &quot;request&quot; |&gt; deserialize_type in 
        let examples = e |&gt; List.map ~f:(fun ex -&gt; (ex |&gt; member &quot;inputs&quot; |&gt; to_list |&gt; List.map ~f:unpack,
                                                    ex |&gt; member &quot;output&quot; |&gt; unpack)) in
        let maximum_frontier = j |&gt; member &quot;maximumFrontier&quot; |&gt; to_int in
        let name = j |&gt; member &quot;name&quot; |&gt; to_string in
  
        let task =
          (try
             let special = j |&gt; member &quot;specialTask&quot; |&gt; to_string in
             match special |&gt; Hashtbl.find task_handler with
             | Some(handler) -&gt; handler (j |&gt; member &quot;extras&quot;)
             | None -&gt; (Printf.eprintf &quot; (ocaml) FATAL: Could not find handler for %s\n&quot; special;
                        exit 1)
           with _ -&gt; supervised_task) ~timeout:timeout name task_type examples
        in 
        (task, maximum_frontier))
    in
  </code></pre>
  <p>There are a number of different <code>task_handler</code>s, but the one I happen to be concerned with is defined as follows:</p>
  <pre><code>(fun extras ?timeout:(timeout = 0.001) name ty examples -&gt;
      let open Yojson.Basic.Util in
  
      let cost_matters =
        try
          extras |&gt; member &quot;costMatters&quot; |&gt; to_bool
        with _ -&gt; assert false
      in 
  
      let by = match examples with
        | [([0],y)] -&gt;
          Bigarray.(Array1.of_array int8_unsigned c_layout (Array.of_list y))
        | [([1],y)] -&gt;
          Bigarray.(Array1.of_array int8_unsigned c_layout (Array.of_list y))
        | _ -&gt; failwith &quot;not a turtle task&quot; in
      { name = name    ;
        task_type = ty ;
        log_likelihood =
          (fun p -&gt;
             try
               match run_recent_logo ~timeout p with
               | Some(bx,cost) when (LogoLib.LogoInterpreter.fp_equal bx by 0) -&gt;
                 (if cost_matters then (0.-.cost)*.10. else 0.)
               | _ -&gt; log 0.
             with (* We have to be a bit careful with exceptions if the
                   * synthesized program generated an exception, then we just
                   * terminate w/ false but if the enumeration timeout was
                   * triggered during program evaluation, we need to pass the
                   * exception on
                  *)
             | UnknownPrimitive(n) -&gt; raise (Failure (&quot;Unknown primitive: &quot;^n))
             | EnumerationTimeout  -&gt; raise EnumerationTimeout
             | _                   -&gt; log 0.0)
      });;
  </code></pre>
  <p>The author also uses <code>;;</code> in a lot of files..another quirk.</p>