How to resolve F # Type Reference Errors?

I went through my books and I have googled until I have run out of searches, but I still cannot find an example or answer to this problem:

The following code does not compile because the Effect type and Affect type were not declared during the declaration of Entity. Therefore, I do not understand how this can be circumvented.

In C ++, this problem is solved by declaring the prototype in h files, and then includes the h file. In C #, this is never a problem. So how is this allowed in F #?

#light type Entity = { Name:string; Affects:List<Affect>; //Compile error: The type Affect is not defined Effects:List<Effect>; //Compile error: the type Effect is not defined } type Effect = { Name:string; //A function pointer for a method that takes an Entity and returns an Entity ApplyEffect:Entity -> Entity; } type Affect = { Name:string; //A List of Effects that are applied by this Affect Object EffectList:List<Effect>; //A function pointer to return an Entity modified by the listed Effects ApplyAffect:Entity->Entity; } 

The main goal here is that an entity of type Entity should be able to enumerate Affects, which it can apply to Type Entity entities. An entity can also list the effects that have been applied to it. Thus, the "current" state of the object is detected by adding all the effects against the initial state of the object.

Thank you for your time,

- Adam Lenda

+6
f # mutual-recursion
source share
1 answer

I believe this is the correct answer:

http://langexplr.blogspot.com/2008/02/defining-mutually-recursive-classes-in.html

So...

 type Entity = { Name:string; Affects:List<Affect>; Effects:List<Effect>; } and Effect = { Name:string; ApplyEffect:Entity -> Entity; } and Affect = { Name:string; EffectList:List<Effect>; ApplyAffect:Entity->Entity; } 
+13
source share

All Articles