孤独的猫

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

F# Interactive can be used to write code at the console or in a window in Visual Studio.

To run F# Interactive from the console, run fsi.exe. For information about command line options available, see F# Interactive Options.

To run F# Interactive through Visual Studio 2010, you can click the appropriate toolbar button labeled F# Interactive, or use the keys Ctrl+Alt+F. Doing this will open the interactive window, a tool window running an F# Interactive session. You can also select some code that you want to run in the interactive window and hit the key combination ALT+ENTER. F# Interactive starts in a tool window labeled F# Interactive.

Whether you are using the console or Visual Studio 2010, a command prompt appears and the interpreter awaits your input. You can enter code just as you would in a code file. To compile and execute the code, enter two semicolons (;;) to terminate a line or several lines of input.

F# Interactive attempts to compile the code and, if successful, it executes the code and prints the signature of the types and values that it compiled. If errors occur, the interpreter prints the error messages.

Code entered in the same session has access to any constructs entered previously, so you can build up programs. An extensive buffer in the tool window allows you to copy the code into a file if needed.

When run in Visual Studio, F# Interactive runs independently of your project, so, for example, you cannot use constructs defined in your project in F# Interactive unless you copy the code for the function into the interactive window.

You can control the F# Interactive command line arguments (options) by adjusting the settings. On the Tools menu, select Options..., and then expand F# Tools. The two settings that you can change are the F# Interactive options and the path to the fsi.exe executable, which you can use if you have a version of fsi.exe from a different version of F# that you want to run.

Scripts use the file extension .fsx or .fsscript. Instead of compiling source code and then later running the compiled assembly, you can just run fsi.exe and specify the filename of the script of F# source code, and F# interactive reads the code and executes it in real time.

When you are compiling code in F# Interactive, whether you are running interactively or running a script, the symbol INTERACTIVE is defined. When you are compiling code in the compiler, the symbol COMPILED is defined. Thus, if code needs to be different in compiled and interactive modes, you can use preprocessor directives for conditional compilation to determine which to use.

Some directives are available when you are executing scripts in F# Interactive that are not available when you are executing the compiler. The following table summarizes directives that are available when you are using F# Interactive.

 

Directive

Description

#help

Displays information about available directives.

#I

Specifies an assembly search path in quotation marks.

#load

Reads a source file, compiles it, and runs it.

#quit

Terminates an F# Interactive session.

#r

References an assembly.

#time ["on"|"off"]

By itself, #time toggles whether to display performance information. When it is enabled, F# Interactive measures real time, CPU time, and garbage collection information for each section of code that is interpreted and executed.

When you specify files or paths in F# Interactive, a string literal is expected. Therefore, files and paths must be in quotation marks, and the usual escape characters apply. Also, you can use the @ character to cause F# Interactive to interpret a string that contains a path as a verbatim string. This causes F# Interactive to ignore any escape characters.

One of the differences between compiled and interactive mode is the way you access command line arguments. In compiled mode, use GetCommandLineArgs. In scripts, use fsi.CommandLineArgs.

The following code illustrates how to create a function that reads the command line arguments in a script and also demonstrates how to reference another assembly from a script. The first code file, MyAssembly.fs, is the code for the assembly being referenced. Compile this file with the command line: fsc -a MyAssembly.fs and then execute the second file as a script with the command line: fsi --exec file1.fsx test

 
Copy
// MyAssembly.fs
module MyAssembly
let myFunction x y = x + 2 * y
 
Copy
// file1.fsx
#r "MyAssembly.dll"

printfn "Command line arguments: "

for arg in fsi.CommandLineArgs do
    printfn "%s" arg

printfn "%A" (MyAssembly.myFunction 10 40)

The output is as follows:

 
Copy
file1.fsx
test
60
posted on 2011-11-19 23:19  孤独的猫  阅读(679)  评论(0编辑  收藏  举报