如何解决 F# 类型引用错误?
我已经浏览了我的书,并且用谷歌搜索,直到我用完搜索词,但我仍然找不到这个问题的示例或答案:
以下代码无法编译,因为类型 Effect 和声明 Entity 时尚未声明类型 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;
}
这里的基本目标是实体类型的对象应该能够列出它可以应用于实体类型的对象的影响。 实体还可以列出已应用于其的效果。 这样,通过将所有效果与原始实体状态折叠来找到实体的“当前”状态。
感谢您抽出时间,
--Adam Lenda
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信这是正确的答案:
http://langexplr .blogspot.com/2008/02/defining-mutually-recursive-classes-in.html
所以...
I believe this is the correct answer:
http://langexplr.blogspot.com/2008/02/defining-mutually-recursive-classes-in.html
so...