简体   繁体   English

F#匹配 - >

[英]F# match with ->

I want to make something like it (Nemerle syntax) 我想做类似的东西(Nemerle语法)

def something =
match(STT)
    | 1 with st= "Summ"
    | 2 with st= "AVG" =>
        $"$st : $(summbycol(counter,STT))"

on F# so is it real with F#? 在F#上,F#真的如此吗?

There is no direct support for that but you can mimic the effect like this as well: 没有直接的支持,但你也可以模仿这样的效果:

let 1, st, _ | 2, _, st = stt, "Summ", "AVG"
sprintf "%s %a" st summbycol (counter, stt)

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: 在F#中没有对此的直接支持,但您可以定义参数化的活动模式来执行此操作:

let (|Let|) v e = (v, e)

match stt with 
| Let "Summ" (st, 1) 
| Let "AVG" (st, 2) -> srintf "%s ..." st

The string after Let is a parameter of the pattern (and is passed in as value of v ). Let之后的字符串是模式的参数(并作为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. 然后该模式返回一个包含绑定值和原始值的元组(因此您可以匹配元组的第二个参数中的原始值。

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

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