when using FsToolkit.ErrorHandling and looking at this example:
testCase "Happy Path Result" <| fun () ->
let actual : Validation<int, string> = validation {
let! a = Ok 3
and! b = Ok 2
and! c = Ok 1
return a + b - c
}
what is the meaning of and!? it looks like let! gets mapped to Result.bind, but I don't see a different behavior between let! and and!
as far as I can see, it looks like replacing and! with let! keeps the same behavior.
I've just started dabbling in computation expressions and things are starting to make sense, although I don't really understand why we have different nomenclature between the F# keywords like let and the actual functions like bind so it's adding a layer of confusion. So the answer to this may be obvious :)
That's the new support for "applicative computation expressions" introduced in F# 5.0.
The difference between let!
and and!
is that let!
allows a value to depend on a prior result, while and!
does not. This is useful for cases where your computation is an applicative, but not a full monad, or when you want to perform independent steps in a computation in parallel.
For example, with let!
you could write this:
let! a = Ok 3
let! b = Ok (2 * a) // Allowed: value of b depends on a
But you can't write this:
let! a = Ok 3
and! b = Ok (2 * a) // Not allowed: value of b depends on a