`type private MyRecord = {...}` 和 `type MyRecord = private {...}` 有什么区别
What's the difference between `type private MyRecord = {...}` and `type MyRecord = private {...}`
有什么区别
type private MyRecord =
{ id : int }
和
type MyRecord = private
{ id : int }
根据我的阅读:
- 在第一种情况下,类型是私有的,只能在文件内访问。
- 在第二种情况下,类型是 public 但所有字段都设为私有。我们可以通过属性暴露重要的。
如评论所示,您已经大功告成了。有趣的是,F# Language Specification 中并未明确提及这一事实,但可以从尝试将记录字段设为私有时产生的错误推断出来,例如type MyRecord = { private id : int }
Accessibility modifiers are not permitted on record fields. Use 'type
R = internal ...' or 'type R = private ...' to give an accessibility
to the whole representation.
实际上,如果您将 "the whole representation" 设为私有,则您无法构造记录,也无法访问可访问性修饰符范围之外的字段。
有什么区别
type private MyRecord =
{ id : int }
和
type MyRecord = private
{ id : int }
根据我的阅读:
- 在第一种情况下,类型是私有的,只能在文件内访问。
- 在第二种情况下,类型是 public 但所有字段都设为私有。我们可以通过属性暴露重要的。
如评论所示,您已经大功告成了。有趣的是,F# Language Specification 中并未明确提及这一事实,但可以从尝试将记录字段设为私有时产生的错误推断出来,例如type MyRecord = { private id : int }
Accessibility modifiers are not permitted on record fields. Use 'type R = internal ...' or 'type R = private ...' to give an accessibility to the whole representation.
实际上,如果您将 "the whole representation" 设为私有,则您无法构造记录,也无法访问可访问性修饰符范围之外的字段。