我通常有一个哦是的时刻写F#,当我意识到我需要一个额外的价值。这通常通过向被传递的元组添加另一个值来容易地完成。然而,这意味着各种地图/排序/收集/等。需要更新,特别是函数fst / snd只处理长度为2的元组。
这不是一个大问题,但在探索式开发过程中我会写一个帮助来缓解烦恼:
let inline get2(t:^ a)=成员get_Item2:unit - > string)(t,())) let inline get2a(t:^ a)=(^ a :( member Item2:string)t)但是,两个版本都不工作。第一个, get2 ,不会编译,用期望1表达式,得到2。第二个, get2a ,将编译,但随后不能在元组上使用:类型'(int *字符串)'不支持任何名为'get_Item2' 。
有没有办法做到这一点,不涉及大量的重载?
因为 ItemX 静态约束对F#元组不起作用的原因是因为 System.Tuple< ; _,...,_> 只是元组的编码形式,而不是编译器使用的静态表示形式。请参阅规范中的 6.3.2元组表达式。但是,通过一些工作,您可以获得给定元组的运行时编码,如下所示:
open System //像get2a,但通用返回类型 let inline get2b(t:^ a)=(^ a:(member Item2:'b)t) let x =(1,2) let y =(1,2,3) get2b(box x:?> Tuple& int>) get2b(box y:?> Tuple< int,int,int>)
I commonly have a "oh yeah" moment writing F# when I realize I need an extra value somewhere. This is generally easily done by adding another value to the tuple being passed around. However, this means that various maps/sorts/collects/etc. need updating, and in particular the functions fst/snd only work on tuples of length 2.
It's not a huge issue, but it's annoying enough during exploratory development that I though I'd write a helper to alleviate the annoyance:
let inline get2 (t:^a) = (^a : (member get_Item2 : unit -> string) (t, ())) let inline get2a (t:^a) = (^a : (member Item2 : string) t)However, both versions do not work. The first, get2, won't compile, with "Expected 1 expressions, got 2". The second, get2a, will compile, but subsequently can't be used on tuples: "The type '(int * string)' does not support any operators named 'get_Item2'".
Is there any way of doing this that doesn't involve lots of overloads? with noisy OverloadID annotations (annotations not required in F# 2.0)
解决方案The reason why ItemX static constraints on F# tuples do not work is because System.Tuple<_,...,_> is only the encoded form of tuples, and not the static representation used by the compiler. See 6.3.2 Tuple Expressions in the specification.
However, with a little work, you can obtain the runtime encoding of a given tuple like so:
open System //like get2a but generic return type let inline get2b (t:^a) = (^a : (member Item2 : 'b) t) let x = (1,2) let y = (1,2,3) get2b (box x :?> Tuple<int,int>) get2b (box y :?> Tuple<int,int,int>)
更多推荐
F#成员对元组的约束
发布评论