fork download
  1. open System
  2. open Microsoft.FSharp.Reflection
  3. open Microsoft.FSharp.Quotations
  4. open Microsoft.FSharp.Quotations.Patterns
  5.  
  6. type Box<'a, 'b> = Box of 'a * 'b
  7.  
  8. let iToS (Box (i, v)) = Box ((sprintf "%A" i), v)
  9.  
  10. let convert (f:Quotations.Expr<'a -> 't>) (v:'a) : 't =
  11. let methi e =
  12. let rec methi' e =
  13. match e with
  14. | Call (x, mi, y) -> mi
  15. | Lambda (_, body) -> methi' body
  16. | _ -> failwith <| sprintf "not a function %A" e
  17. methi' e
  18.  
  19. let apply f v =
  20. let m = methi f
  21. m.Invoke(null, [|box v|])
  22.  
  23. apply f v :?> 't
  24.  
  25. let r10 = (convert <@ iToS @>) (Box (1, 1))
  26. printfn "%A" r10
  27.  
  28. let r20 = (convert <@ iToS @>) (Box (1.0, 1.0))
  29. printfn "%A" r20
Success #stdin #stdout 0.06s 31432KB
stdin
Standard input is empty
stdout
Box ("1",1)
Box ("1.0",1.0)