F# 类属性与显式字段
谁能帮我举例说明 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
前两种形式的用途有限,如果您需要非常明确地了解类中字段的布局方式,那么它们很有用。例如,您可能会将类型传递给非托管代码,该代码需要按一定顺序包含一定数量的字段,或者您可能会将其传递给使用字段反射的 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.