F#成员对元组的约束

编程入门 行业动态 更新时间:2024-10-28 17:27:32
本文介绍了F#成员对元组的约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我通常有一个哦是的时刻写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#成员对元组的约束

本文发布于:2023-11-29 22:18:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647794.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:成员

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!