本文介绍了F#类型测试模式匹配:分解元组对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
很好奇为什么我不能这样做:
Just curious why I can't do this:
let myFn (data : obj) =
match data with
| :? (string * string) as (s1, s2) -> sprintf "(%s, %s)" s1 s2 |> Some
| :? (string * string * int) as (s1, s2, i) -> sprintf "(%s, %s, %d)" s1 s2 i |> Some
| _ -> None
怎么来?
推荐答案
请参见 F#规范,第7.3节作为模式"
See F# spec, section 7.3 "As patterns"
as模式的格式为 pat as ident
这意味着您需要在 as
之后使用标识符:
Which means you need to use an identifier after as
:
let myFn (data : obj) =
match data with
| :? (string * string) as s1s2 -> let (s1, s2) = s1s2 in sprintf "(%s, %s)" s1 s2 |> Some
| :? (string * string * int) as s1s2i -> let (s1, s2, i) = s1s2i in sprintf "(%s, %s, %i)" s1 s2 i |> Some
| _ -> None
这篇关于F#类型测试模式匹配:分解元组对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!