f# - FS0074 : The type referenced through 'C.CRecord' is defined in an assembly that is not referenced. 您必须添加对程序集 'C' 的引用

标签 f# class-visibility

假设我有一个名为“C”的库 (.NETStandard 2.0),它定义了一个名为“CRecord”(记录)的类型。

假设我从名为“B”的 .NET 4.7.2 库中使用这个库。有一个使用“CRecord”类型的“B”类型。

现在假设我有一个名为“A”的可执行 .NET4.7.2 项目,它使用“B”中的类型,但并不真正使用“C”中的组件。

编译“A”时,你认为你会得到下面这个编译器错误吗?

Error FS0074: The type referenced through 'C.CRecord' is defined in an assembly that is not referenced. You must add a reference to assembly 'C'. (FS0074) (A)

经过多次测试,我开始意识到答案是“视情况而定”。关键似乎在于“B”中类型的实际实现方式。示例:

namespace B

module BModule =

    type BSimpleRecord =
        { Baz: uint32; C: CRecord }

    type BComplexRecord = private { baz: uint32; c: CRecord } with
        member public this.Baz = this.baz
        member internal this.C = this.c
        static member public New(baz, c) =
            { baz = baz; c = c }

    type BComplexType internal (baz: uint32, c: CRecord) =
        member val Baz = baz with get
        member val internal C = c with get

您会说哪种类型会产生编译器错误?最合乎逻辑的想法是回答 BSimpleRecord,因为它根本不会隐藏 C 元素。正确的?对吧????

作为引用,这是 A 和 C 代码(以及返回 B 实例的 B 函数):

namespace C

type CRecord =
    { Foo: int; Bar: string }

namespace B

module BModule =

    let FunctionThatReturnsBSimpleRecord () =
        let c = { Foo = 1; Bar = "" }
        let b = { Baz = 1u; C = c }
        b

    let FunctionThatReturnsBComplexRecord () =
        let c = { Foo = 1; Bar = "" }
        let baz = 10u
        let b = BComplexRecord.New(baz, c)
        b

    let FunctionThatReturnsBComplexType () =
        let c = { Foo = 1; Bar = "" }
        let baz = 10u
        let b = BComplexType(baz, c)
        b

(* Program.fs of A project below *)

[<EntryPoint>]
let main argv =
    let b = B.BModule.FunctionThatReturnsBComplexType()
    printfn "%s" (b.Baz.ToString())
    0 // return an integer exit code

好吧,我的 F-sharpers,答案实际上是最意想不到的:当你使用 BSimpleRecord 时你没有得到编译器错误(而你得到另一个错误两个)。

所以我仍然想知道为什么?为什么???我完全不明白这一点。也许这是 F# 编译器中的错误?

最佳答案

常规 F# 记录类型由属性标识:

[<CompilationMapping(SourceConstructFlags.RecordType)>]

每当您更改记录的访问级别时,SourceConstructFlags 将变为

RecordType | NonPublicRepresentation

对于常规类(如 BComplexType),它只是

ObjectType

这是您的工作案例与其他案例之间的主要区别。

我无法弄清楚 F# 编译器中的确切位置,但它将进行汇编探索,除非它是 RecordType - 这很可能发生在 NameResolution.fsresolve 调用之一中。

堆栈跟踪 here . 它发生在 ResolveExprDotLongIdentAndComputeRange(对于 b.Baz)。

关于f# - FS0074 : The type referenced through 'C.CRecord' is defined in an assembly that is not referenced. 您必须添加对程序集 'C' 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62274013/

相关文章:

for-loop - F# for 循环错误

F# 如何返回具有元组或空值的值

F#如何编写一个按顺序提供计数器编号的函数

Javascript方式设计继承+隐私

java - 如何实现可见性受限的Java图数据结构和类?

c# - 在 .NET 上更快地解析数字

f# - Newtonsoft.Json 将某些项目序列化两次

c# - 我可以将 C# 扩展方法的可见性限制为同一程序集中的类吗?

Java 接口(interface)的可见性降低,只是没有

c++ - 在私有(private)嵌套类上使用 using 指令