colinfang
colinfang

Reputation: 21737

Can we extend a module in the same file

Inorder to interop with c#, I do the following

namespace foo

type a = ...

module myhelper =
    let d = ...

open myhelper

type b = ...

module myhelper = // module name duplication error
    ...

I wanna add more functions which could use type b, so I have to write them after type b declaration, but I don't want to create a new module each time, what should I do?

Global module could help, But, I don't want my c# code to access my type using mymodule.a. So I didn't use a global module but a global namespace

Is there any better structure ideas.

Upvotes: 3

Views: 192

Answers (1)

Daniel
Daniel

Reputation: 47904

Such subtle interdependencies tend to make your library intractable as it grows. F# helps by preventing this, or at least forcing it to be explicit.

Based on your abbreviated example, you could either define an interface (that myhelper targets and b implements) or use mutually recursive types:

type MyHelper =
  static member M1() = ()
  static member M2() = let b = B() in b.M4()
and B() =
  member x.M3() = MyHelper.M1()
  member x.M4() = ()

Upvotes: 1

Related Questions