41

Is it possible to split an F# module across files?

According to the book I have it is, but the book is probably outdated (Foundations of F#)

0

5 Answers 5

17

Apparently not:

C:\temp\Tim>type 1.fs 2.fs

1.fs


#light
module Module

let sayHello1 = printfn "Hello, "

2.fs


#light
module Module

let sayHello2 = printfn "world!"

C:\temp\Tim>fsc 1.fs 2.fs
Microsoft F# Compiler, (c) Microsoft Corporation, All Rights Reserved
F# Version 1.9.6.2, compiling for .NET Framework Version v2.0.50727

2.fs(2,1): error FS0191: An implementation of the file or module Module has already been given.

Update: the error has changed in F# 4.0, it is now:

error FS0248: Two modules named 'Module' occur in two parts of this assembly

where Module is the fully qualified name of your assembly, including the namespace part.

4
  • 2
    That's to bad, because I'm doing data parsing and there are many different data structures so I'm generating a very very very long F# file...
    – TimothyP
    Apr 27, 2009 at 14:01
  • Time to break it up into regular .NET classes instead of modules? Or more than one F# module? Apr 27, 2009 at 14:20
  • 3
    Unfortunately F# doesn't support partial classes, so you can't even split a regular .NET class over several source files. Apr 27, 2009 at 14:21
  • Why not use namespaces as the common parent if you want to do this? The effect is the same: fully qualified module name works as expected. Only keep in mind that a module declaration is IDENTICAL to declaring a static class. Which, actually, explain why you can't declare two identically named modules, unless you have them in different namespaces. Jul 12, 2022 at 21:30
8

The type extensions are cool, and hopefully they will allow to be cross file, while still being intrinsic. If you do a type extension in the same file, it compiles to one class, and the extension has access to private members and so on. If you do it in another file, it's just an "optional" extension, like C# static extension methods. (Even though the F# specs say differently.)

I'd be surprised if this isn't addressed at some point, if only for designer support. If intrinsic type extensions could be anywhere in the assembly, that'd be pretty slick.

Another option, which might not be what you want, is to create a type and a module, call the module the same name, and then add the ModuleSuffix flag to it:

type Foo() = 
    static member Bar = 1

[<CompilationRepresentationAttribute(CompilationRepresentationFlags.ModuleSuffix)>]
module Foo =
    let Baz = 2

printfn "%d %d" Foo.Bar Foo.Baz

This is used in the F# libraries, so they can have a type List or whatever, along with tons of helper stuff in a module.

1
  • 3
    This is neat within F#, but if I want to access these types from the outside via reflection, they come up as types "Foo" and "FooModule". Based on the accepted response, there's no way to get the two to show in the same type, is there? Jan 26, 2011 at 22:25
6

Like Kurt says, you can add extension methods to types, and thus

// File1.fs
namespace Foo

type Mine() =
    static member f1 () = ()

then

// File2.fs
type Foo.Mine with
    static member f2() = ()

Foo.Mine.    // both f1 and f2 here

Since it's a class and not a module, you lose the ability to do 'open Mine' (but gain the ability to overload); thus this may or may not be an acceptable alternative for you depending on exactly what you're doing.

1
  • This is true for Intellisense within File2.fs, but when you load the type via reflection (e.g., typeof(Foo.Mine).GetMethods(), f2 doesn't show. Thanks for the tip, though! Jan 26, 2011 at 21:33
6

On one of my projects the goal was to place file operations Cp and Rm to separate modules, but do not require user to open two namespaces for both tasks.

open Xake.FileTasks
...
do! Cp "*/*.exe" "deploy/*.exe"
do! Rm "*/*.exe"

Here're my modules:

namespace Xake.FileTasks

[<AutoOpen>]
module RmImpl =
    let Rm filemask target =
...

and the other one:

namespace Xake.FileTasks

[<AutoOpen>]
module CpImpl =
    let Cp filemask target =
...
4

I sometimes split a type over several places, like this:

module Foo

type Partial = Bar | BarInt of int

module Bar

type Foo.Partial with
    member x.Extend = 5


let b = Foo.Bar.Extend

where modules Foo and Bar are in different files.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.