gpt4 book ai didi

f# - 如何解决 F# 类型引用错误?

转载 作者:行者123 更新时间:2023-12-04 13:35:47 25 4
gpt4 key购买 nike

我已经翻阅了我的书,并且在谷歌上搜索,直到我用完搜索词,但我仍然找不到这个问题的示例或答案:

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

在 C++ 中,这个问题是通过在 h 文件中声明原型(prototype)然后包含 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;
}

这里的基本目标是实体类型的对象应该能够列出它可以应用于实体类型的对象的影响。实体还可以列出已应用到它的效果。这样,通过将所有效果与原始实体状态折叠起来,就可以找到实体的“当前”状态。

感谢您的时间,

——亚当·伦达

最佳答案

我相信这是正确的答案:

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;
}

关于f# - 如何解决 F# 类型引用错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/848585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com