Error handling
All expressions with the exception of collections can have an error value.
For instance:
let x: string = String.Read(Http.Get("http://broken/location"))
Because the URL http://broken/location
is invalid, the String.Read
returns an error.
To check if the string contains a value or an error, use:
let
x: string = String.Read(Http.Get("http://broken/location"))
in
Try.IsError(x) // true
It's also possible to build an error value. For instance, here is a function that returns the double of a number if it is less than 10, or an error otherwise.
let f(v: int) = if (v < 10) then v * 2 else Error.Build("v must be less than 10")
This function can be then be used as:
let
f(v: int) = if (v < 10) then v * 2 else Error.Build("v must be less than 10")
in
Try.IsSuccess(f(5)) // true
... while ...
let
f(v: int) = if (v < 10) then v * 2 else Error.Build("v must be less than 10")
in
Try.IsSuccess(f(100)) // false