简体   繁体   中英

Pattern matching in let binding in F#

I wanna do:

let Some(x) = bar in ...

but I can't do this unless I do

let Some(x) as idontcare = bar in ...

is there a better way to say "I don't care about the whole pattern, just match the inside"

(I would use _ but that doesn't parse so I am using __ instead)

Yes I know this is partial, I just am doing a quick script.

Edit: Also this is just an example with a builtin sum type, so Option.get is not generic; plus I want this to be inline like the Haskell let-bindings.

let Some(x) = bar

defines a new function Some , shadowing the existing constructor. Instead, you want:

let (Some(x)) = bar

You could use a match :

match bar with | Some(x) -> ...

if you're trying to match an option specifically you could use Option.get :

bar |> Option.get |> ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM