Does F# support interdependent classes in separate files?
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 Operat开发者_StackOverflow中文版ors =
...
// Here, we are referencing BoxedValue.
static member add(BoxedValue l, BoxedValue r)
...
So, we have this set of dependencies:
Ideally, we would like to split each of these into its own file.
Is it possible in F# to have cross-file circular dependencies?
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)
精彩评论