Cannot open namespace in Fsharp script file

浪尽此生 提交于 2019-12-11 03:45:59

问题


When in a separate Fsharp project in a SomeLib.fs file the following code is compiled:

namespace SomeNameSpace
type SomeType =
    member this.SomeMember = "Some member"

and you want to reference and use this type in a script file like:

#I @"c:/pathToDll/"
#r "SomeLib.dll"

This is not possible, although the path to the dll is correct and I checked everything. Also when the SomeLib.fs file is in the same project and referenced by #load, you still cannot open the namespace.

I know you can put the type in a module, but I cannot do this as the type has te be used as a Wcf service type.


回答1:


After a lot of experimental work and surprisingly little info on the internet or in F# books I found out the following:

// Cannot use a relative path
//#I @"bin\Debug"
// Have to use a absolute path
#I @"C:\Development\FSharpNameSpaceTest\SomeCSharpLib\bin\Debug"
// But I can reference a Csharp dll lib
#r "SomeCSharpLib.dll"

// I cannot add a reference to an external F# library dll
// #I @"C:\Development\FSharpNameSpaceTest\NameSpace\bin\Debug"
// #r "NameSpace.dll"

// If I directly load the external fs file, it works"
#load @"C:\Development\FSharpNameSpaceTest\NameSpace\SomeNameSpace.fs"
#load "Library1.fs"

// Namespaces in both the local and the external fs files can only be openend if every single file is loaded instead of referencing the dll.
// Referencing a C# dll is no problem

open FSharpNameSpaceTest
open SomeCSharpLib
open NameSpace

I do not know if this is the most optimal approach but it works. What I will do is, I will create a fsx file for every project that loads the individual fs files in that project and then I will load that fsx file in the fsx file that references the project.

I still find this all very confusing and counterintuitive. But that might be my limited knowledge of the innerworks of F#

Edit: And the right and complete answer is, I didn't implement a default constructor. Still, if you do not wan't to do this, the above approach is an alternative. Thanks to Marc Sigrit.




回答2:


If on Windows, the slashes in the import directive should be replaced by backslashes.



来源:https://stackoverflow.com/questions/16927764/cannot-open-namespace-in-fsharp-script-file

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