Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one resolve F# Type Reference Errors?

I've been through my books, and I've googled until I've ran out of search terms, yet I still can't find an example or answer to this problem:

The following code does not compile because the type Effect and the type Affect have not been declared at the time Entity is declared. So what I don't understand is how do to work around this.

In C++, this problem is solved via a prototype declaration in h files and then including the h file. In C# it is never an issue. So how is it resolved 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 underlying goal here is that an object of type Entity should be able to list the Affects it can apply to objects of Type Entity. Entity can also list the Effects that have been applied to it. This way the "current" state of an entity is found by folding all of the Effects against the original entity state.

Thank you for your time,

--Adam Lenda

like image 965
Adam Lenda Avatar asked May 11 '09 15:05

Adam Lenda


People also ask

What are the resolved parts of force F?

The force F can now be resolved into two components Fx and Fy along the x and y axes and hence, the components are called rectangular components.

How do we resolve vectors?

A vector can be resolved into many different vectors, for resolution of vectors. For Example: Let us consider two numbers, say, 4 and 6, which is further added to obtain 10. Further, now 10 is broken or resolved. However, the number 10 can also be resolved into many other numbers like –10 = 5 + 5; 10 = 3 + 7 etc.

What is the formula for resultant force?

The formula for the resultant force vector is given by Newton's Second law. This law states that the force is equal to mass times acceleration, or F=ma.


1 Answers

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;
    }
like image 107
Adam Lenda Avatar answered Sep 24 '22 17:09

Adam Lenda