如何解决F#类型参考错误?

Ada*_*nda 6 f# mutual-recursion

我已经浏览了我的书,直到我用完了搜索条件,但我还是找不到这个问题的例子或答案:

以下代码无法编译,因为在声明Entity时尚未声明类型Effect和类型Affect.所以我不明白的是如何解决这个问题.

在C++中,这个问题是通过h文件中的原型声明然后包含h文件来解决的.在C#中,它永远不是问题.那么如何在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;
    }
Run Code Online (Sandbox Code Playgroud)

这里的基本目标是Entity类型的对象应该能够列出它可以应用于Type Entity对象的Affects.实体还可以列出已应用于它的效果.这样,通过将所有效果折叠到原始实体状态来找到实体的"当前"状态.

感谢您的时间,

--Adam Lenda

Ada*_*nda 13

我相信这是正确的答案:

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

所以...

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;
    }
Run Code Online (Sandbox Code Playgroud)