Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions docs/content/interactive.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ First, we need to reference the libraries that contain F# interactive service:
*)

#r "FSharp.Compiler.Service.dll"
open Microsoft.FSharp.Compiler.SourceCodeServices
open Microsoft.FSharp.Compiler.Interactive.Shell

(**
Expand Down Expand Up @@ -81,7 +82,7 @@ and other top-level statements.
let evalInteraction text =
fsiSession.EvalInteraction(text)
(**
The two functions take string as an argument and evaluate (or execute) it as F# code. The code
The two functions each take a string as an argument and evaluate (or execute) it as F# code. The code
passed to them does not require `;;` at the end. Just enter the code that you want to execute:
*)
evalExpression "42+1"
Expand All @@ -97,6 +98,49 @@ let evalScript scriptPath =
File.WriteAllText("sample.fsx", "let twenty = 10 + 10")
evalScript "sample.fsx"

(**
Catching errors
------------------

``EvalExpression``, ``EvalInteraction`` and ``EvalScript`` are awkward if the
code has type checking warnings or errors, or if evaluation fails with an exception.
In these cases you can use ``EvalExpressionNonThrowing``, ``EvalInteractionNonThrowing``
and ``EvalScriptNonThrowing``. These return a tuple of a result and an array of ``FSharpErrorInfo`` values.
These represent the errors and warnings. The result part is a ``Choice<_,_>`` between an actual
result and an exception.

The result part of ``EvalExpression`` and ``EvalExpressionNonThrowing`` is an optional ``FSharpValue``.
If that value is not present then it just indicates that the expression didn't have a tangible
result that could be represented as a .NET object. This siutation shouldn't actually
occur for any normal input expressions, and only for primitives used in libraries.
*)

File.WriteAllText("sample.fsx", "let twenty = 'a' + 10.0")
let result, warnings = fsiSession.EvalScriptNonThrowing "sample.fsx"

// show the result
match result with
| Choice1Of2 () -> printfn "checked and executed ok"
| Choice2Of2 exn -> printfn "execution exception: %s" exn.Message

(**
Gives:

execution exception: Operation could not be completed due to earlier error
*)

// show the errors and warnings
for w in warnings do
printfn "Warning %s at %d,%d" w.Message w.StartLineAlternate w.StartColumn

(**
Gives:

Warning The type 'float' does not match the type 'char' at 1,19
Warning The type 'float' does not match the type 'char' at 1,17
*)


(**
Type checking in the evaluation context
------------------
Expand Down Expand Up @@ -129,8 +173,8 @@ You can also request declaration list information, tooltip text and symbol resol
*)
open Microsoft.FSharp.Compiler

let identToken = Parser.tagOfToken(Parser.token.IDENT(""))
checkResults.GetToolTipTextAlternate(1, 2, "xxx + xx", ["xxx"], identToken) // a tooltip
// get a tooltip
checkResults.GetToolTipTextAlternate(1, 2, "xxx + xx", ["xxx"], FSharpTokenTag.IDENT)

checkResults.GetSymbolUseAtLocation(1, 2, "xxx + xx", ["xxx"]) // symbol xxx

Expand Down
2 changes: 1 addition & 1 deletion src/fsharp/ErrorLogger.fs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ let rec findOriginalException err =


/// Thrown when we stop processing the F# Interactive entry or #load.
exception StopProcessing of string
exception StopProcessing of exn option


(* common error kinds *)
Expand Down
2 changes: 1 addition & 1 deletion src/fsharp/fsc.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2099,7 +2099,7 @@ type InProcCompiler() =
let exitCode = ref 0
let exiter =
{ new Exiter with
member this.Exit n = exitCode := n; raise (StopProcessing "") }
member this.Exit n = exitCode := n; raise (StopProcessing None) }
try
typecheckAndCompile(argv, false, true, exiter, loggerProvider, None, None)
with
Expand Down
Loading