如何将记录元组解构为私有值

发布于 2025-01-07 09:55:10 字数 357 浏览 1 评论 0原文

如何使用模块级 let 绑定将两个私有值绑定到元组中的记录?

type private T = {F:int}
let private a = {F=1}
let private b, private c = {F=2}, {F=3}

在此示例中,a 工作正常,但 bc 的绑定均失败并出现错误:

错误 FS0410:类型 'T' 比它所使用的值、成员或类型 'val patternInput : T * T' 更难访问

How do you use a module-level let binding to bind two private values to records in a tuple?

type private T = {F:int}
let private a = {F=1}
let private b, private c = {F=2}, {F=3}

In this example, a works fine, but the bindings for b and c each fail with the error:

error FS0410: The type 'T' is less accessible than the value, member or type 'val patternInput : T * T' it is used in

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

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

发布评论

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

评论(1

小霸王臭丫头 2025-01-14 09:55:10

要了解为什么这不起作用,请使用 Reflector 对其进行反编译。您正在创建一个元组并立即解构它。无论如何,在调试模式下,中间元组都会创建为封闭类型的内部字段(模式匹配的实现细节)。当然,这使得它比 T 更容易访问,而 Tprivate。我很好奇这是否是一个错误(看起来是这样)。

编辑

它变得更奇怪......

以下无法编译(有语法错误),但如果类型是公共的并且使用let而不是let private,则可以正常编译。

type private T = {a:int; b:int}
let private t = {a=0; b=0}
let private {a=a; b=b} = t //syntax error

type private U() = class end
let private ul = [U()]
let private [u] = ul //syntax error

我认为可以肯定地说模块级私有 let 绑定中的解构存在一些问题。

To see why this doesn't work, decompile it with Reflector. You're creating a tuple and immediately destructuring it. In debug mode anyway, the intermediate tuple is created as an internal field of the enclosing type (an implementation detail of the pattern match). Of course, that makes it more accessible than T, which is private. I'm curious to know if this is a bug (seems like it).

EDIT

It gets weirder...

The following fails to compile (with syntax error), but compiles fine if the types are public and let, instead of let private, is used.

type private T = {a:int; b:int}
let private t = {a=0; b=0}
let private {a=a; b=b} = t //syntax error

type private U() = class end
let private ul = [U()]
let private [u] = ul //syntax error

I think it's safe to say destructuring in module-level private let bindings has some problems.

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