簡體   English   中英

F#,名稱空間,模塊,fs和fsx

[英]F#, namespaces, modules, fs and fsx

我知道有關F#中的模塊和命名空間的其他 問題 ,但他們現在沒有幫助我。

我有一個項目

Utilities.fs

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

Functions.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時, error FS0039: The namespace or module 'Utilities' is not defined

我已經嘗試在腳本文件的頂部添加相同的命名空間,但它不喜歡這樣。

奇怪的是后台編譯器沒有對我大喊大叫。

這似乎有效,但它是正確的approch?

#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#規范的一部分

在名稱空間聲明組中,如果任何前面的名稱空間聲明組或引用的程序集對此名稱空間有貢獻,則會隱式打開名稱空間本身,例如

 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 

但是,這僅打開由前面的名稱空間聲明組構成的名稱空間。

鑒於FSI對命名空間的理解有限,不與FSI交互。 具體來說,我希望您的示例中的“第二個#load”打開,例如FSI_000N+1的命名空間版本,而之前的代碼在FSI_000N 這可能解釋了為什么明確的open互動會修復它; 在嘗試(隱式)稍后引用它之前,你將現有的,沒有FSI_000N內容提升到頂層。

我在這方面也比較新,但是當我在fsx文件中測試時,這對我有用:

#if INTERACTIVE
#r @"C:\Program Files\FSharpPowerPack-2.0.0.0\bin\FParsec.dll"
#r @"C:\Program Files\FSharpPowerPack-2.0.0.0\bin\FParsecCS.dll"
#endif

open FParsec.Primitives  
open FParsec.CharParsers

然后是我使用這些庫的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM