简体   繁体   English

F#中的let绑定中的模式匹配

[英]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; 编辑:这只是一个内置和类型的例子,所以Option.get不是通用的; plus I want this to be inline like the Haskell let-bindings. 另外我希望它像Haskell let-bindings一样内联。

let Some(x) = bar

defines a new function Some , shadowing the existing constructor. 定义一个新函数Some ,遮蔽现有构造函数。 Instead, you want: 相反,你想要:

let (Some(x)) = bar

You could use a match : 你可以使用match

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

if you're trying to match an option specifically you could use Option.get : 如果你想特别匹配一个选项,你可以使用Option.get

bar |> Option.get |> ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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