Search code examples
f#pattern-guards

How to use matching on argument level, is it possible? Guards?


for example

let When true d = d
let foo = () |> When false

So I've got side effect I don't like because it's error: MatchFailureException

I know that I can have good side effect here: let foo = if false then ()

But warning Incomplete pattern matches on this expression. tells me like I can add what I need. I just don't know how or if it possible? Else way why I even can use values as arguments?

btw: I know that I can write When b d = if b then d else () but question is more general.

added explain in haskell

    let wh :: Bool -> IO () -> IO ()
        wh True f = f
        wh False f = return ()

Solution

  • The fact that this is allowed is more of a quirk of the spec, and is really so you can do things like

    let (a,b) = 1,2
    

    Your question isn't particularly clear, but I think you are looking for something like

    let 1 |2 = 1
    

    Essentially the syntax is the same as after match except you don't need the first |

    Side note, this allows for the craziest piece of F# code that I wrote in a while:

    let 1 = 2
    

    This will compile (with a warning) but fail at runtime.