I just spent a little while hunting down an interesting problem in a little F# app. I had a bunch of code in a single file and I was going to structure it a bit and move certain parts into separate files. I started out from some code like this:
namespace Sturm.MyNamespace
type ICommandLine = begin
abstract member Parts: string list
end
type IPlugin =
abstract member CanHandle: ICommandLine -> bool
abstract member Handle: ICommandLine -> unit
type blah =
val mutable dummy: int
interface IPlugin with
member x.CanHandle(commandLine) = false
member x.Handle(commandLine) = x.dummy <- 1
end
new() = { dummy = 0 }
Yeah, I know — useless code. The point is that there are dependencies between the three types. So I was trying to move the interfaces into a different file, and suddenly the type blah
couldn’t find the interfaces anymore. Interesting. The solution to this is simple, but looks a bit stupid. At the start of my new file, I had of course repeated the namespace declaration namespace Sturm.MyNamespace
, but that is not enough. Even though that puts all the types in the same namespace, it is still necessary to explicitely open the namespace as well, if you’re going to access elements that have been defined elsewhere for the same namespace. So I need this header in the file with the class type:
namespace Sturm.MyNamespace
open Sturm.MyNamespace
While I can see the logic behind that, it doesn’t seem like a very useful approach… perhaps I’ll figure out the reasons later, right now I’m glad I’ve found a solution.