小编Ada*_*nda的帖子

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

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

以下代码无法编译,因为在声明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 …
Run Code Online (Sandbox Code Playgroud)

f# mutual-recursion

6
推荐指数
1
解决办法
993
查看次数

标签 统计

f# ×1

mutual-recursion ×1