F# - the Thing With the Namespaces

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

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
  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.

Sorry, this blog does not support comments.

I used various blog hosting services since this blog was established in 2005, but unfortunately they turned out to be unreliable in the long term and comment threads were lost in unavoidable transitions. At this time I don't want to enable third-party services for comments since it has become obvious in recent years that these providers invariably monetize information about their visitors and users.

Please use the links in the page footer to get in touch with me. I'm available for conversations on Keybase, Matrix, Mastodon or Twitter, as well as via email.