Skip to content

Instantly share code, notes, and snippets.

@1tgr
Created September 18, 2012 10:18
Show Gist options
  • Select an option

  • Save 1tgr/3742439 to your computer and use it in GitHub Desktop.

Select an option

Save 1tgr/3742439 to your computer and use it in GitHub Desktop.
Inspect the last value evaluated in FSI
// Run this as: fsi --use:it.fsx --quiet
open System
open System.Reflection
open Microsoft.FSharp.Compiler.Interactive
let (getSavedItType : unit -> Type, getSavedIt : unit -> obj) =
let t = typeof<IEventLoop>
let t = t.Assembly.GetType(t.Namespace + ".RuntimeHelpers", true)
let bf = BindingFlags.Static ||| BindingFlags.NonPublic
let itTypeMethod = t.GetMethod(name = "GetSavedItType", bindingAttr = bf)
let itMethod = t.GetMethod(name = "GetSavedIt", bindingAttr = bf)
(fun () -> unbox (itTypeMethod.Invoke(null, [| |]))), (fun () -> itMethod.Invoke(null, [| |]))
let printIt () =
match getSavedItType () with
| t when t = typeof<unit> -> printfn "No value"
| t -> printfn "Got value: %A : %O" (getSavedIt ()) t
(*
1 + 1
;;
printIt ();;
let one = 1 + 1
;;
printIt ();;
let rec fib n =
if n < 2 then
1
else
fib (n - 2) + fib (n - 1)
;;
printIt ();;
fib 6
;;
printIt ();;
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment