为什么我不能匹配字符串或字符串列表类型
Why can't I match on a string or string list type
我是 F# 的新手,正在尝试使用 string
或 string list
的联合类型,然后在函数内对其进行匹配。
我在第二种情况下收到错误 This expression was expected to have type 'stringOrList' but here has type ''a list'
。
虽然我不明白为什么。难道第一种情况不匹配字符串大小写,所以第二种情况不匹配字符串列表大小写吗?
type stringOrList = S of string | L of string list
let mockFetcher (fileOrFiles : stringOrList) url =
match fileOrFiles with
| S file ->
file
| L firstFile :: rest ->
firstFile
如上所写,您的案例如下:(L firstFile) :: rest
,由于优先规则。
应该是L (firstFile :: rest)
...并且不要忘记丢失的案例 L []
。
我是 F# 的新手,正在尝试使用 string
或 string list
的联合类型,然后在函数内对其进行匹配。
我在第二种情况下收到错误 This expression was expected to have type 'stringOrList' but here has type ''a list'
。
虽然我不明白为什么。难道第一种情况不匹配字符串大小写,所以第二种情况不匹配字符串列表大小写吗?
type stringOrList = S of string | L of string list
let mockFetcher (fileOrFiles : stringOrList) url =
match fileOrFiles with
| S file ->
file
| L firstFile :: rest ->
firstFile
如上所写,您的案例如下:(L firstFile) :: rest
,由于优先规则。
应该是L (firstFile :: rest)
...并且不要忘记丢失的案例 L []
。