Home When to implement `Zero` member in Computation Expression?
Reply: 0

When to implement `Zero` member in Computation Expression?

user857
1#
user857 Published in June 20, 2018, 1:19 am

Why can't I use pattern matching function in computation expressions without implement Zero member?

For example, could somepone explain why it permits allowing pattern matching expression but not pattern matching function?

type MaybeBuilder() =
    member __.Bind (x, f) = match x with Some a -> f a | None -> None
    member __.Return x = Some x

let maybe = MaybeBuilder()

// Errors: FS0708 This control construct may only be used
// if the computation expression builder defines a 'Zero' method
maybe { Some 1 |> function Some x -> return x | None -> return 0 }
maybe { Some 1 |> fun x -> match x with Some x' -> return x' | None -> return 0 }

// Ok
maybe { match Some 1 with Some x -> return x | None -> return 0 }
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.336969 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO