10

シナリオは次のとおりです。

  1. Visual Studio を開きます。これはVS2010 Proで行われました。
  2. Visual Studio 内で F# Interactive を開く
  3. fsx ファイルでプロジェクトを開く
    注: プロジェクトと fsx ファイルはE:\<directories>\fsharp-tapl\arith
  4. fsx ファイルから F# Interactive にコマンドを送信する

    > System.Environment.CurrentDirectory;; 
    val it : string = "C:\Users\Eric\AppData\Local\Temp"
    

    私は一時ディレクトリを期待していませんでしたが、それは理にかなっています。

    > #r @"arith.exe"
    Examples.fsx(7,1): error FS0082: Could not resolve this reference. 
    Could not locate the assembly "arith.exe". 
    Check to make sure the assembly exists on disk. 
    If this reference is required by your code, you may get compilation errors. 
    (Code=MSB3245)
    
    Examples.fsx(7,1): error FS0084: Assembly reference 'arith.exe' was not found 
    or is invalid
    

    #r コマンド エラーは、現在 F# Interactive が arith.exe の場所を認識していないことを示しています。

    > #I @"bin\Debug"
    --> Added 'E:\<directories>\fsharp-tapl\arith\bin\Debug' 
    to library include path
    

    そこで、F# Interactive に arith.exe の場所を伝えます。パスは絶対パスではなく、プロジェクトのサブパスであることに注意してください。F# Interactive に arith プロジェクトの場所を伝えていません。 E:\<directories>\fsharp-tapl\arith

    > #r @"arith.exe"
    --> Referenced 'E:\<directories>\fsharp-tapl\arith\bin\Debug\arith.exe'
    

    また、F# Interactive は、正しい絶対パスを報告する arith.exe を正しく検出します。

    > open Main
    > eval "true;" ;;
    true
    val it : unit = ()
    

    これにより、arith.exe が正しく検出され、ロードされ、動作することが確認されます。

では、現在のディレクトリは役に立たないため、F# Interactive #I コマンドはどのようにしてプロジェクト パスを認識したのでしょうか?

私が本当に求めているのは、F# Interactive 内からプロジェクトへのパスを取得する方法E:\<directories>\fsharp-tapl\arithです。

編集

> printfn __SOURCE_DIRECTORY__;;
E:\<directories>\fsharp-tapl\arith
val it : unit = ()
4

1 に答える 1

19

F# Interactive では、検索する既定のディレクトリはソース ディレクトリです。を使用して簡単に照会できます__SOURCE_DIRECTORY__

この動作は、相対パスを使用できるようにするのに非常に便利です。多くの場合、fsxファイルと同じフォルダーにfsファイルがあります。

#load "Ast.fs"
#load "Core.fs"

相対パスを参照する場合、F# Interactive は常に暗黙的なソース ディレクトリを開始点として使用します。

#I ".."
#r ... // Reference some dll in parent folder of source directory
#I ".."
#r ... // Reference some dll in that folder again

次回の参照のために古いディレクトリを覚えておきたい場合は、#cd代わりに次を使用する必要があります。

#cd "bin"
#r ... // Reference some dll in bin
#cd "Debug"
#r ... // Reference some dll in bin/Debug
于 2013-02-03T15:36:21.770 に答える