开发者

Does FSI deal well with multi module F# projects?

Does FSI deal well with multi module/file F# projects? Consider the following project:

module.fs:

module Xyz

let add x y = x + y

Program.fs:

module Program

open Xyz    
let result = add 1 2

selecting and running the las开发者_开发问答t 2 lines of Program.fs will yield the following FSI error:

Program.fs: error FS0039: The namespace or module 'Xyz' is not defined

What is the problem here?


So, let's say I have the following project structure:

A.fs
B.fs
C.fs
D.fs
E.fs

I want to run some functions of E.fs. E.fs makes use of all the other .fs files, so I have one open for each one of them. If later on I want to also run some code from any one of the other files, I'll have to repeat the process fo any file that was not #loaded before.

From your suggestions, it seems like to make my E.fs file run on FSI I'll either have to create a separate .fsx file or have a separate

#if INTERACTIVE
#load "..."

for each module I'll use which is IMO quite redundant. Am I missing something here, or is this a clear violation of both KISS and DRY principles?


The error will be because you have not loaded the file - it works fine for me doing

 $ fsi --load:module.fs --load:Program.fs

Microsoft (R) F# 2.0 Interactive build 2.0.0.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

For help type #help;;

[Loading /suphys/jpal8929/fsbug/module.fs
 Loading /suphys/jpal8929/fsbug/Program.fs]

namespace FSI_0002
  val add : int -> int -> int


namespace FSI_0002
  val result : int

> 


There are a few options, some of them already mentioned by Brian and jpalmer, so I'll just try to summarize:

  • If you have just a few files, then you can "load" them, which means that the source gets sent to F# Interactive and is compiled in the FSI console. This approach is useful if you want to test a part of a larger project.

    To do this, you can either use --load:file.fs command line option or you can write #load "file.fs" in F# Interactive (or in a Script.fsx file that is sent to F# Interactive using --load). To manage more files, you can write a script Load.fsx that loads all the files you need.

  • If you have a larger project (e.g. a library) and want to use it or test it from F# Interactive, then you can compile the library using fsc.exe, reference it from F# Interactive and use its public types/functions from the console

    To do this, you can either use -r:MyLib.dll command line option, or you can write #r "MyLib.dll in F# Interactive (or in an fsx script file).


Are you aware of #load? Are you aware of #r (after compiling other code into a DLL)? http://msdn.microsoft.com/en-us/library/dd233175.aspx


Create an .fsx file that begins:

#load "module.fs" "Program.fs"

and run that from FSI first to get your code loaded.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜