@@ -31,6 +31,7 @@ First, we need to reference the libraries that contain F# interactive service:
31
31
*)
32
32
33
33
#r " FSharp.Compiler.Service.dll"
34
+ open Microsoft.FSharp .Compiler .SourceCodeServices
34
35
open Microsoft.FSharp .Compiler .Interactive .Shell
35
36
36
37
(**
@@ -81,7 +82,7 @@ and other top-level statements.
81
82
let evalInteraction text =
82
83
fsiSession.EvalInteraction( text)
83
84
(**
84
- The two functions take string as an argument and evaluate (or execute) it as F# code. The code
85
+ The two functions each take a string as an argument and evaluate (or execute) it as F# code. The code
85
86
passed to them does not require `;;` at the end. Just enter the code that you want to execute:
86
87
*)
87
88
evalExpression " 42+1"
@@ -97,6 +98,49 @@ let evalScript scriptPath =
97
98
File.WriteAllText( " sample.fsx" , " let twenty = 10 + 10" )
98
99
evalScript " sample.fsx"
99
100
101
+ (**
102
+ Catching errors
103
+ ------------------
104
+
105
+ ``EvalExpression``, ``EvalInteraction`` and ``EvalScript`` are awkward if the
106
+ code has type checking warnings or errors, or if evaluation fails with an exception.
107
+ In these cases you can use ``EvalExpressionNonThrowing``, ``EvalInteractionNonThrowing``
108
+ and ``EvalScriptNonThrowing``. These return a tuple of a result and an array of ``FSharpErrorInfo`` values.
109
+ These represent the errors and warnings. The result part is a ``Choice<_,_>`` between an actual
110
+ result and an exception.
111
+
112
+ The result part of ``EvalExpression`` and ``EvalExpressionNonThrowing`` is an optional ``FSharpValue``.
113
+ If that value is not present then it just indicates that the expression didn't have a tangible
114
+ result that could be represented as a .NET object. This siutation shouldn't actually
115
+ occur for any normal input expressions, and only for primitives used in libraries.
116
+ *)
117
+
118
+ File.WriteAllText( " sample.fsx" , " let twenty = 'a' + 10.0" )
119
+ let result , warnings = fsiSession.EvalScriptNonThrowing " sample.fsx"
120
+
121
+ // show the result
122
+ match result with
123
+ | Choice1Of2 () -> printfn " checked and executed ok"
124
+ | Choice2Of2 exn -> printfn " execution exception: %s " exn.Message
125
+
126
+ (**
127
+ Gives:
128
+
129
+ execution exception: Operation could not be completed due to earlier error
130
+ *)
131
+
132
+ // show the errors and warnings
133
+ for w in warnings do
134
+ printfn " Warning %s at %d ,%d " w.Message w.StartLineAlternate w.StartColumn
135
+
136
+ (**
137
+ Gives:
138
+
139
+ Warning The type 'float' does not match the type 'char' at 1,19
140
+ Warning The type 'float' does not match the type 'char' at 1,17
141
+ *)
142
+
143
+
100
144
(**
101
145
Type checking in the evaluation context
102
146
------------------
@@ -129,8 +173,8 @@ You can also request declaration list information, tooltip text and symbol resol
129
173
*)
130
174
open Microsoft.FSharp .Compiler
131
175
132
- let identToken = Parser.tagOfToken ( Parser.token.IDENT ( " " ))
133
- checkResults.GetToolTipTextAlternate( 1 , 2 , " xxx + xx" , [ " xxx" ], identToken ) // a tooltip
176
+ // get a tooltip
177
+ checkResults.GetToolTipTextAlternate( 1 , 2 , " xxx + xx" , [ " xxx" ], FSharpTokenTag.IDENT )
134
178
135
179
checkResults.GetSymbolUseAtLocation( 1 , 2 , " xxx + xx" , [ " xxx" ]) // symbol xxx
136
180
0 commit comments