F# 类属性与显式字段

发布于 2024-11-08 12:05:34 字数 678 浏览 0 评论 0原文

谁能帮我举例说明 F# 中何时需要显式字段?例如,以下三个课程有何用处

type MyClass =
    val a : int
    val b : int
    new(a0, b0) = { a = a0; b = b0; }

,或者

type MyClass() =
    [<DefaultValue>] val mutable a: int
    [<DefaultValue>] val mutable b: int
    member this.Setab( a0: int, b0: int) =
       a<- a0
       b<- b0

相比

type MyClass(a0:int,b0:int) =
    member x.a = a0
    member x.b = b0

与我只能理解最后一个课程 。谢谢。

编辑:以下问题是前两个概念是必要的示例:FileHelpers 类型中的字段顺序

Can anyone help me with example on when an explicit field is necessary in F#? for instance, how would the following three classes be all useful

type MyClass =
    val a : int
    val b : int
    new(a0, b0) = { a = a0; b = b0; }

or

type MyClass() =
    [<DefaultValue>] val mutable a: int
    [<DefaultValue>] val mutable b: int
    member this.Setab( a0: int, b0: int) =
       a<- a0
       b<- b0

comparing with

type MyClass(a0:int,b0:int) =
    member x.a = a0
    member x.b = b0

I can only understand the last class. thanks.

EDIT: the following question is an example that the first two notions are necessary: Order of fields in a type for FileHelpers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸠书 2024-11-15 12:05:34

前两种形式的用途有限,如果您需要非常明确地了解类中字段的布局方式,那么它们很有用。例如,您可能会将类型传递给非托管代码,该代码需要按一定顺序包含一定数量的字段,或者您可能会将其传递给使用字段反射的 api。

The first two forms have some limited uses, they are useful if you need to be very explicity about the way the fields in a class are laid out. For example you maybe passing the type to unmanaged code which expects a certain number of fields in a certain or order, or you maybe passing it to an api that uses reflection over the fields.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文