gpt4 book ai didi

F#、命名空间、模块、fs 和 fsx

转载 作者:行者123 更新时间:2023-12-03 10:16:43 27 4
gpt4 key购买 nike

我知道 other questions关于 F# 中的模块和命名空间,但它们现在对我没有帮助。

我有一个项目

实用程序.fs

namespace Company.Project.Namespace
module Utilities =
//stuff here

函数.fs
namespace Company.Project.Namespace
open Utilities

module Functions =
//stuff here

我正在尝试在 fsx 中测试它们:
#load "Utilities.fs"
#load "Functions.fs"

这给了我 error FS0039: The namespace or module 'Utilities' is not defined当我尝试使用 Alt-Enter 将其发送到 FSI 时.

我尝试在脚本文件的顶部添加相同的命名空间,但它不喜欢这样。

奇怪的是后台编译器并没有对我大喊大叫。

这似乎有效,但它是正确的方法吗?
#load "Utilities.fs"
open Company.Project.Namespace
#load "Functions.fs"

某处是否有“引用”FSharp 项目,其中包含如何集成所有这些东西的示例:命名空间、模块、类、脚本文件、测试等?

最佳答案

我不是 FSI 专家,但一些实验表明命名空间仅受 #load 支持。声明(不是通过典型的交互 - 通过 Alt-Enter 向 VFSI 发送命名空间声明组不起作用),并且不同的交互贡献不同的“实例”。例如,使用代码文件

namespace Foo

type Bar() =
member this.Qux() = printfn "hi"

namespace Other

type Whatever() = class end

namespace Foo

module M =
let bar = new Bar()
bar.Qux()

如果我 #load我不止一次得到它,例如
> [Loading C:\Program.fs]
hi

namespace FSI_0002.Foo
type Bar =
class
new : unit -> Bar
member Qux : unit -> unit
end
namespace FSI_0002.Other
type Whatever =
class
new : unit -> Whatever
end
namespace FSI_0002.Foo
val bar : Bar

> #load @"C:\Program.fs";;
> [Loading C:\Program.fs]
hi

namespace FSI_0003.Foo
type Bar =
class
new : unit -> Bar
member Qux : unit -> unit
end
namespace FSI_0003.Other
type Whatever =
class
new : unit -> Whatever
end
namespace FSI_0003.Foo
val bar : Bar

> new Foo.Bar();;
> val it : Foo.Bar = FSI_0003.Foo.Bar

请注意,似乎 FSI_0003.Foo.Bar 掩盖了 FSI_0002 版本。

所以我在想 F# 规范中说的部分

Within a namespace declaration group, the namespace itself is implicitly opened if any preceding namespace declaration groups or referenced assemblies contribute to this namespace, e.g.

namespace MyCompany.MyLibrary 

module Values1 =
let x = 1

namespace MyCompany.MyLibrary

// Implicit open of MyCompany.MyLibrary bringing Values1 into scope

module Values2 =
let x = Values1.x

However this only opens the namespace as constituted by preceding namespace declaration groups.



鉴于 FSI 对命名空间的了解有限,不与 FSI 交互。具体来说,我希望您的示例中的“第二个#load”会打开,例如 FSI_000N+1的命名空间版本,而之前的代码在 FSI_000N .这也许可以解释为什么显式 open交互修复它;你带来了现有的、无阴影的 FSI_000N在稍后尝试(隐式)引用它之前,先将其填充到顶层。

关于F#、命名空间、模块、fs 和 fsx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2354984/

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