【发布时间】:2020-03-06 02:39:29
【问题描述】:
我有一些嵌套的可区分联合
type Job = Sniff | Guard
type Dog = Chihuahua | GermanShepherd of Job
这是一个接受Dog 并返回string 的函数。
let dogPrinter d =
match d with
| Chihuahua -> ""
| GermanShepherd g ->
match g with
| Sniff -> ""
| Guard -> ""
我可以将第一个match 转换为function 语法:
let dogPrinter = function
| Chihuahua -> ""
| GermanShepherd g ->
match g with
| Sniff -> ""
| Guard -> ""
如何将第二个match 转换为function?
【问题讨论】:
-
这似乎是XY problem。 为什么你想这样做?它是完全可读的。不过,您可以使用嵌套模式来避免第二次匹配。
-
为什么?只是想知道如何。嵌套模式是什么意思?你能发布答案吗?
标签: f# pattern-matching