Question

I want to load a file from a .fsx script into into the F# Interactive Session but I can't use #load since I only want to load it if a certain condition is true.

Is there a function like FSI.LoadFile or something instead of the compiler directive?

Was it helpful?

Solution

Looking at the source code (fsi.fs, line 1710 here

        | IHash (ParsedHashDirective("load",sourceFiles,m),_) ->
            fsiDynamicCompiler.EvalSourceFiles (istate, m, sourceFiles, lexResourceManager),Completed

Now, some of these parameters are probably easy to fake - in particular sourceFiles and m. I suspect that the other parameters are harder to fake.

I suspect that you are not trying to solve the problem in a good way. An alternative solution may be to do something like

let conditionally_load fname cond = 
    if cond then System.IO.File.Copy(fname,"dummy.fs")
    else System.IO.File.Create("dummy.fs")
conditionally_load "something.fs" true
#load "dummy.fs"

Although you might need a ;; before the #load to ensure that the function runs before the #load

OTHER TIPS

What you are looking to do is tricky, since you want the decision on whether to load additional code to be taken at evaluation time. A solution to this would be to modify the shell so that script loading can be scheduled at the next iteration of the REPL. In essence, your FSI.LoadFile function would simply add the path to a global queue, in the form of a ParsedHashDirective("load",["foo.fsx"], ...) expression.

The queue could then be prepended to the actions list next time the ExecInteractions method is called in line 1840. This should work fine, but clearly you won't be getting any intellisense support.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top