访问外部程序集中结构的公共只读成员

发布于 2024-08-03 05:15:38 字数 1182 浏览 6 评论 0原文

当我使用 F# 读取 C# 程序集中定义的结构类型的 public readonly 成员时,出现奇怪的错误。

// C#: compile to Lib.dll
namespace Lib
{
    public class MyClass { public readonly int ReadonlyFoo; }

    public struct MyStruct
    {
        public readonly int ReadonlyFoo;
        public int WriteableFoo;
    }
}

// F#: compile to Client.exe
open Lib
let myClass = new MyClass()
printfn "MyClass.ReadonlyFoo = %x" myClass.ReadonlyFoo

let myStruct = new MyStruct()
printfn "MyStruct.WriteableFoo = %x" myStruct.WriteableFoo
printfn "MyStruct.ReadonlyFoo = %x" myStruct.ReadonlyFoo

当我使用 F# 1.9.6.16 编译 Client.exe 时,最后一行给出错误:

"The address of the variable 'copyOfStruct' may not be used at this point"

The web is 无用。人们可以读取类的不可变成员,并且可以读取结构的可变成员,但无法读取结构的不可变成员,这似乎很奇怪。解决方法很简单,但我很好奇:这是编译器中的错误吗?

编辑:我向[电子邮件受保护]提交了错误报告< /强>

I'm getting a strange error when I use F# to read a public readonly member of a struct type defined in a C# assembly.

// C#: compile to Lib.dll
namespace Lib
{
    public class MyClass { public readonly int ReadonlyFoo; }

    public struct MyStruct
    {
        public readonly int ReadonlyFoo;
        public int WriteableFoo;
    }
}

// F#: compile to Client.exe
open Lib
let myClass = new MyClass()
printfn "MyClass.ReadonlyFoo = %x" myClass.ReadonlyFoo

let myStruct = new MyStruct()
printfn "MyStruct.WriteableFoo = %x" myStruct.WriteableFoo
printfn "MyStruct.ReadonlyFoo = %x" myStruct.ReadonlyFoo

When I compile Client.exe with F# 1.9.6.16, the last line gives the error:

"The address of the variable 'copyOfStruct' may not be used at this point"

The web is useless as of the time of this writing. It seems odd that one can read an immutable member of a class, and one can read a mutable member of a struct, but one can't read an immutable member of a struct. A workaround is easy enough, but I'm curious: is this a bug in the compiler?

Edit: I submitted a bug report to [email protected]

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

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

发布评论

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

评论(1

你如我软肋 2024-08-10 05:15:38

通常,当人们说“它看起来像是编译器中的错误”时,实际上是“我不知道我在做什么”的代码。然而,在这种情况下,它看起来确实像一个错误。

F# 编译器在幕后制作结构的副本,以防结构发生变异。 (这就是为什么即使您定义了一个具有可变字段的结构,您也必须先将该结构的实例属性设置为可变,然后才能更新其字段。) 似乎幕后发生的特殊魔法忘记了“只读”结构字段。

虽然互联网和 StackOverflow 是寻求有关 F# 相关问题的帮助的好地方,请通过发送电子邮件 [电子邮件受保护]

Normally when people say 'it looks like a bug in the compiler' that is code for 'I don't know what I'm doing'. In this situation however, it does look to be like a bug.

The F# compiler makes a copy of structs behind the scenes in case they get mutated. (This is why even if you define a struct with mutable fields you must attribute the instance of that struct as mutable before you can update its fields.) It appears that the special magic going on behind the scenes forgets about 'readonly' struct fields.

While the internet and StackOverflow are a great place to ask for help about F#-related issues, please do let the F# team know about any bugs you find by emailing [email protected].

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