首页 > 解决方案 > F# 结构元组的 fst 和 snd 的等价物?

问题描述

如果我使用引用元组,则编译:

let plot(x: int, y: int) = ()
let point = 3, 4
plot(fst point, snd point)

但是,如果我使用的是结构元组......

let plot(x: int, y: int) = ()
let point = struct (3, 4)
plot(fst point, snd point)

...我得到编译器错误,One tuple type is a struct tuple, the other is a reference tuple

我应该怎么办?

标签: f#

解决方案


ToTuple()System.TupleExtensionsfor中有一个扩展方法ValueTuple<T1, T2...>

你可以打电话:

plot (point.ToTuple())

至于fst, snd,它们必然是System.Tuple<>,所以也许你可以定义一个替代方案:

let fstv struct (a,_) =  a
let sndv struct (_,b) =  b

推荐阅读