F#与->匹配 [英] F# match with ->
问题描述
我想做类似的事情(Nemerle语法)
I want to make something like it (Nemerle syntax)
def something =
match(STT)
| 1 with st= "Summ"
| 2 with st= "AVG" =>
$"$st : $(summbycol(counter,STT))"
在F#上,F#是真的吗?
on F# so is it real with F#?
推荐答案
如果我对您的理解正确,那么您希望将某些值分配给变量作为模式的一部分. F#中对此没有直接支持,但是您可以定义一个执行此操作的参数化活动模式:
If I understand you correctly, you'd like to assign some value to a variable as part of the pattern. There is no direct support for this in F#, but you can define a parameterized active pattern that does that:
let (|Let|) v e = (v, e)
match stt with
| Let "Summ" (st, 1)
| Let "AVG" (st, 2) -> srintf "%s ..." st
Let
之后的字符串是模式的参数(并作为v
的值传递).然后,该模式将返回一个包含绑定值和原始值的元组(以便您可以在元组的第二个参数中匹配原始值.
The string after Let
is a parameter of the pattern (and is passed in as value of v
). The pattern then returns a tuple containing the bound value and the original value (so you can match the original value in the second parameter of the tuple.
这篇关于F#与->匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!