TryEvalExpression throws an FsiEvaluationException exception
toburger opened this issue · 3 comments
Don't know if this is by design, but I think a method prefixed with Try should not throw an exception.
For example I load an external script and I'm not sure if the script has a binding "app" defined.
I load the script and call:
fsiSession.TryEvalExpression "app"I get an FsiEvaluationException if app is not defined.
I think a nicer way to eval the expression (and to have the same signature like EvalExpression) is:
match fsiSession.TryEvalExpression<WebPart> "app"
| Some wp -> // do something with wp
| None -> // couldn't get app valueNow there remains one problem, that it is not possible to find out if app wasn't defined or the type wasn't of type WebPart.
Thanks for the feedback!
Do you by chance know on which cases the underlying API returns None (https://github.com/fsharp/FSharp.Compiler.Service/blob/master/src/fsharp/fsi/fsi.fsi#L158). It seems like it either returns Some or throws an exception. This would help me to decide on this.
Now there remains one problem, that it is not possible to find out if app wasn't defined or the type wasn't of type WebPart.
Would the following work?
adding another extension method to fsiSession Handle<'a> : (string -> 'a) -> string -> HandleResult<'a>
with
type HandledResult<'a> =
| InvalidExpressionType of FsiExpressionTypeException
| InvalidCode of FsiEvaluationException
| Result of 'aand usage:
match fsiSession.Handle fsiSession.EvalExpression<WebPart> "app" with
| InvalidExpressionType e -> // not of type WebPart
| InvalidCode e -> // couldn't get app value (compiler error, not defined)
| Result r -> // r is the value.as all methods have the string -> 'a signature this would work with all current methods
The signature of EvalExpression in compiler services is a bit strange. I don't know in which circumstances None gets returned. Maybe it's a relict of older days?
Regarding the Discriminated Union for the EvalExpression, that would make good sense!
Sadly my initial suggestion doesn't work:
host.Handle host.EvalExpression<unit -> string> """fun () -> sprintf "blub123" """;;
-------------------------------^^^^^^^^^^^^^^^^
C:\Users\dragon\AppData\Local\Temp\stdin(8,32): error FS0717: Unerwartete Typargumente.
however
host.Handle<unit -> string> host.EvalExpression """fun () -> sprintf "blub123" """does work, so I guess this fixes this issue for now.
The difference between Try no Try is that without Try we do throw an additional exception when FCS returns None (whatever that means)
Edit: fix