The msdn documentation for the Zero
method in computation expressions states that
Called for empty
else
branches ofif...then
expressions in computation expressions.
Assume we're using an identity
computation builder which does not have Zero
defined.
let IdentityBuilder() =
member this.Bind(i, f) = f i
member this.Return(i) = i
let identity = new IdentityBuilder()
The code below is allowed
identity {
printf "Hello World"
return 1
}
However, the following code is not allowed and fails with the compiler error
This control construct may only be used if the computation expression builder defines a 'Zero' method
identity {
if true then printf "Hello World"
return 1
}
Why does the compiler insist on calling Zero
for else
branches? What is the intuition behind this?