gpt4 book ai didi

c# - F# 属性与 C# 属性

转载 作者:太空狗 更新时间:2023-10-29 23:17:14 27 4
gpt4 key购买 nike

在开发 F# 应用程序时,我有一个包含 Lazy<'T> 类型属性的类型.

显然,F# 处理属性语法糖的方式(与 C# 方式相反)的一个有趣的副作用(请原谅双关语)是属性的 getter 和 setter 可能返回/接受不同的 类型。 (至少,当我编写利用此观察结果的代码时,Visual Studio 没有提示。)

例如,这样做对我有好处:

let lazyValue = lazy 0

member this.Value
with get () =
lazyValue.Value
and set _lazyVal =
lazyValue <- _lazyVal

...这样值返回一个int , 但只接受 Lazy<int> .

我想知道的是,从理论上、惯用语和实践上反对以这种方式做事。这是 F# 势利小人会嗤之以鼻的事情吗?这个(面向对象的实现)明显违反了一些函数式编程经验法则吗?这种方法是否已被证明会在大规模应用程序中引起问题?如果是这样,为什么/如何?

最佳答案

也许这是一个错误,“当 [您] 编写利用此观察的代码时,Visual Studio 没有提示”。参见 Using F# Option Type in C#

对链接的问题注释的回答中的评论:

From section 8.13.1 of the spec: If a property member has both a getter and a setter, and neither is an indexer, then the signatures of both getter and setter must imply the same property type

关于c# - F# 属性与 C# 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8650773/

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