F# FSI, Change working directory

女生的网名这么多〃 提交于 2020-01-04 05:12:09

问题


I have the following file located in a "New Folder" of Desktop:

// File location: "C:\Users\my_user_name\Desktop\New folder\AddOne.fs"
// 
module internal AddOneModule

let AddOneFunction x = x + 1

I can access this file by using #load on the full path name using FSI F# Interactive.

Microsoft (R) F# Interactive version 4.1
Copyright (c) Microsoft Corporation. All Rights Reserved.

For help type #help;;

> #load "C:\Users\my_user_name\Desktop\New folder\AddOne.fs";;
//[Loading C:\Users\my_user_name\Desktop\New folder\AddOne.fs]
//namespace FSI_0002
//  val AddOneFunction : x:int -> int

> open AddOneModule;;
> AddOneFunction 100;;
//  val it : int = 101

How do I change the working directory so that I can access the file using relative path?

F# interactive:how to display/change current working directory

I tried something similar to the post above, but FSI still tries to find the file in the Temp folder:

(RESET FSI)

Microsoft (R) F# Interactive version 4.1
Copyright (c) Microsoft Corporation. All Rights Reserved.

For help type #help;;

> open System;;
> Environment.CurrentDirectory <- @"C:\Users\my_user_name\Desktop\New folder";;
//val it : unit = ()

> #load "AddOne.fs";;

//  #load "AddOne.fs";;
//  ^^^^^^^^^^^^^^^^^

//C:\Users\my_user_name\Desktop\New folder\stdin(3,1): error FS0078: Unable to find 
//the file 'AddOne.fs' in any of
// C:\Users\my_user_name\AppData\Local\Temp

Thank you for your help.


回答1:


Instead of changing the working directory you may achieve the desired using a trick with __SOURCE_DIRECTORY__ built-in identifier.

To begin with, you need a certain anchor point in directory structure. For illustration purposes let's assume you are on Windows and let this anchor point be your user directory, which is defined by %USERPROFILE% environment variable. Place there a script anchorfsi.fsx containing the following single line of code:

#I __SOURCE_DIRECTORY__

That's basically all you need to do. Now, regardless from what location you shoot your fsi using command line fsi --load:%USERPROFILE%\anchorfsi.fsx, you can use relative paths in your scripts and in interactive commands.

Turning to the setup in your question with loading .\desktop\new folder\addone.fs, the following screenshot demonstrates achieving the desired:

Notice how the entered relative path ".\desktop\new folder\addone.fs" was correctly mapped to the absolute one C:\Users\gene\desktop\new folder\addone.fs without any dependency upon the fsi working directory whatsoever.



来源:https://stackoverflow.com/questions/46318803/f-fsi-change-working-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!