微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

f# – 记录与单一案例歧视联盟

什么是Pro和Con使用的

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐