2

I was trying to use a fsi file to allow mutually recursive classes in separate files, but my fsi file did not compile. Below is a simple example which demonstrates the problem.

File program.fs:

module mod1
type first =
    |zero = 0

File File1.fs:

module mod2
type second =
    |zero2 = 0

Compiling with --sig:signature.fsi produces:

#light

module mod1
type first =
  |  zero  =  0

module mod2
type second =
  |  zero2  =  0

Which has an error on the line

type second

Which is

Error   1   Unexpected keyword 'type' in signature file. Expected ':', '=' or other token.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
John Palmer
  • 25,356
  • 3
  • 48
  • 67
  • Is there any way to solve this problem without merging two files in one, as I have questioned here ? http://stackoverflow.com/questions/31086071/forward-type-declaration-with-two-files?noredirect=1#comment50189391_31086071 – ceth Jun 27 '15 at 07:02

1 Answers1

1

You'd think that this is what signature files are for (like C++ header files), but it's not. At least, that's what I thought at first.

The only way to define mutually recursive types in F# is to put them in the same source file and use the and keyword:

module mod1_mod2
    type first =
      | zero = 0

    and second =
      | zero2 = 0
Tim Robinson
  • 53,480
  • 10
  • 121
  • 138
  • So why does the compiler emit a fsi file which does not compile then? – John Palmer Dec 08 '09 at 11:24
  • The main purpose of .fsi files is to define the signature (types and functions) exposed by a module; it's a concept carried over from OCaml. From a .NET interop point of view, marking your types and functions as public/private directly in the source code is more flexible. – Tim Robinson Dec 08 '09 at 11:30
  • Ah, misread your question. I'm not sure why the compiler emits an .fsi file that contains two modules. – Tim Robinson Dec 08 '09 at 11:45