I think the answer from @kvb describes in sufficient detail why you can use templates in fun arguments. This is not a special function - in F # you can use templates wherever you can bind a variable. To show some @kvb examples in other contexts:
Similarly, you can use patterns when writing fun . The match construct is a bit more powerful because you can specify a few sentences.
Now active templates are not really magical. These are ordinary functions with special names. The compiler searches for active templates in scope when it finds a named template. For example, the template you use is just a function:
val (|KeyValue|) : KeyValuePair<'a,'b> -> 'a * 'b
The sample turns the KevValuePair object into a regular F # tuple, which is then matched by a nested pattern (k, v) (which assigns the first element k and the second to v ). The compiler essentially translates your code into:
myDictionary |> Seq.iter (fun _arg0 -> let _arg1 = (|KeyValue|) _arg0 let (k, v) = _arg1 doSomething kv )
Tomas petricek
source share