Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 585
nimish Avatar asked Dec 26 '22 07:12

nimish


2 Answers

let Some(x) = bar

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

let (Some(x)) = bar
like image 163
kvb Avatar answered Dec 27 '22 20:12

kvb


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 |> ...
like image 33
Lee Avatar answered Dec 27 '22 19:12

Lee