gpt4 book ai didi

f# - 完全限定名称,带有导入声明的非限定名称解析不同

转载 作者:行者123 更新时间:2023-12-04 02:23:40 27 4
gpt4 key购买 nike

这有效

open System
let f = Action(fun () -> Unchecked.defaultof<_>)

但是这个
let f = System.Action(fun () -> Unchecked.defaultof<_>)

产生编译错误

Multiple types exist called 'Action', taking different numbers of generic parameters. Provide a type instantiation to disambiguate the type resolution, e.g. 'Action<,,_,,,_,,,_>'.



我知道我可以通过添加类型参数占位符 ( System.Action<_>(...) ) 来修复它,但是知道为什么它们的行为不同吗?

编辑

在规范第 14.1.9 节中找到了这一点:

When a module or namespace declaration group F is opened, items are added to the name environment as follows:

  • Add the type to the TypeNames table. If the type has a CLI-mangled generic name such as List'1 then an entry is added under both List and List'1.


是否为完全限定类型(省略类型参数)复制了这种行为?看起来并非如此。

最佳答案

我同意@James 的观点,这与在 Connect 上提交的错误有关,但我认为情况略有不同。无论如何,我认为这不是预期的行为。你能把它报告给 microsoft dot com 的 fsbugs 吗?

无论如何 - 我做了一些调试,这是我到目前为止发现的:

似乎编译器使用不同的代码路径来解析名称Action和名称 System.Action .在解析另一个时,它会在所有加载的模块(即程序集)中搜索名为 System.Action 的类型。 (见 ResolveLongIndentAsModuleOrNamespaceThen 函数在开源版本的 nameres.fs file 中)。

这会找到 Action 的两个定义。 (一个在 mscorlib 中,另一个在 System.Core 中)。我认为问题来自这样一个事实,即名称解析只是迭代结果 - 它找到第一个(来自 System.Core ),它没有可用的重载(因为它的范围从 Action<_,_,_,_,_> 到具有大约 15 个类型参数)。找到此类型后,它甚至不查看是否有其他类型(在另一个程序集中)可以使用就报告错误。

如果您不引用系统程序集,则 F# 编译器可以很好地解决重载问题。在没有参数的情况下运行编译器会引用默认程序集集,因此这不起作用:

fsc test.fs 

但如果我添加 --noframework标志,然后它编译没有问题:
fsc --noframework test.fs

关于f# - 完全限定名称,带有导入声明的非限定名称解析不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8732769/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com