11

I'm working on IronJS, and one of our source files is getting very long.

Right now, I'm trying to get .NET interop working. I'm adding the TryBinaryOperation method to the Undefined so that C# can use the JavaScript semantics of the Undefined value.

However, this introduces a dependency on the Operators type, which causes a circular dependency.

Runtime.fs:

type BoxedValue() =
    struct
        // Contains IsUndefined and get_Undefined, referencing the Undefined class, below.

...

and type Undefined() =
    inherit DynamicObject()
    ...
    override x.TryBinaryOperation(binder:BinaryOperationBinder, arg:obj, result:obj byref) : bool =
        // Here, we are referencing BoxedValue, above.
        result <- Operators.add(Und, BoxedValue.Box(arg))
        true

...

Operators.fs:

type Operators =
    ...
    // Here, we are referencing BoxedValue.
    static member add(BoxedValue l, BoxedValue r)
        ...

So, we have this set of dependencies:
BoxedValue->Undefined, Undefined->BoxedValue, Undefined->Operators, Operators->BoxedValue

Ideally, we would like to split each of these into its own file.

Is it possible in F# to have cross-file circular dependencies?

John Gietzen
  • 48,783
  • 32
  • 145
  • 190
  • possible duplicate of [Problem with cyclic dependencies between types and functions from different files in F#](http://stackoverflow.com/questions/5404735/problem-with-cyclic-dependencies-between-types-and-functions-from-different-files) – Stephen Swensen Jul 03 '11 at 21:57
  • The answer is "no": http://stackoverflow.com/questions/5404735/problem-with-cyclic-dependencies-between-types-and-functions-from-different-files/5406492#5406492, http://stackoverflow.com/questions/5396465/how-to-organize-f-source-of-large-project-300-classes-in-visual-studio/5396824#5396824 – Stephen Swensen Jul 03 '11 at 21:58
  • It appears someone finally got desperate enough to provide some sort of assistance to this issue: https://marketplace.visualstudio.com/items?itemName=snuup.FSharpFileStructure – Bent Rasmussen May 19 '20 at 10:41

1 Answers1

6

There is no direct way to write circular dependencies between types defined in separate files (in the current version of F#). In general, the way to solve the problem is to break one of the dependencies and allow some form of parameterization. Then you can fill the hole to build the circular reference later.

In your example, you can probably reasonably easily parameterize the Undefined type to take the reference to Operators as a parameter. If you need more functions, then you can use an interface. For just a single function (like Operators.add) you can write something like this:

and type Undefined() =
    inherit DynamicObject()
    ...
    // To be specified by code defined later 
    // (this can either be a function or an interface implementation)
    static let mutable addition = (fun x y -> failwith "not initialized")
    static member SetAddition(f) = addition <- f

    override x.TryBinaryOperation
            (binder:BinaryOperationBinder, arg:obj, result:obj byref) : bool =
        // Here, we are referencing BoxedValue, above.
        result <- addition(Und, BoxedValue.Box(arg))
        true

The code in Operators.fs would provide the implementation:

type Operators =
    ...
    // Static constructor of the `Operators` type
    static do Undefined.SetAddition(Operators.add)
    ....

    // Here, we are referencing BoxedValue.
    static member add(BoxedValue l, BoxedValue r)

The only tricky thing is that you need to make sure that the static constructor of Operators will get called before the Undefined type is used for the first time. This depends on your specific case, but there is usually some way to do that. (There is probably some main type that can run the initialization)

Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
  • 3
    Ok, so I've found the one and only one thing that I hate about F#. But, I hate it so much, I will never use it for anything this big again. – John Gietzen Jul 04 '11 at 07:39
  • @John - The F# team listens to feedback, so you may want to send them a note (e.g. at `fsbugs` at `microsoft.com`, which is mainly for bugs, but will work for feedback too). I agree this is a problem - it doesn't come up too often if the code is written in more functional style (e.g. F# compiler is really huge and has no troubles with this). – Tomas Petricek Jul 05 '11 at 12:47