gpt4 book ai didi

f# - 记录 vs 单例歧视工会

转载 作者:行者123 更新时间:2023-12-04 18:02:20 26 4
gpt4 key购买 nike

使用两者的优点和缺点是什么

type Complex = 
{
real: float;
imag: float;
}

或者
type Complex = 
Complex of
real: float *
imag: float

我对不同情况下的可读性和处理特别感兴趣。
在较小程度上,性能。

最佳答案

使用辅助函数,您可以从两种方法中获得相同的结果。

记录

type ComplexRec = 
{
real: float
imag: float
}

// Conciseness
let buildRec(r,i) =
{ real = r ; imag = i }

let c = buildRec(1.,5.)

// Built-in field acces
c.imag

活接头
type ComplexUnion = 
Complex of
real: float * imag: float

// Built-in conciseness
let c = Complex(1.,5.)

// Get field - Could be implemented as members for a more OO feel
let getImag = function
Complex(_,i) -> i

getImag c

我想联合类型的(频繁)分解可能会影响性能,但我不是这方面的专家。

关于f# - 记录 vs 单例歧视工会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43251264/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com