类型匹配的问题(希望可以修复)F#

类型匹配的问题(希望可以修复)F#,f#,F#,所以我输入了Gitem type Gitem = |Weapon of Weapon |Bomb of Bomb |Monster of Monster |Armour of Armour |Potion of Potion |None 这用于在记录中存储不同类型的对象,很明显,我遇到问题的类型如下: type Monster={ name:string; dVal:float; dType:DamType; h

所以我输入了Gitem

type Gitem = 
    |Weapon of Weapon
    |Bomb of Bomb
    |Monster of Monster
    |Armour of Armour
    |Potion of Potion
    |None
这用于在记录中存储不同类型的对象,很明显,我遇到问题的类型如下:

type Monster={
    name:string;
    dVal:float;
    dType:DamType;
    hVal:float;
    info:string;
    dInfo:string;
    dCry:string;
    Special:bool;
    sItem:obj;
 }        



type Armour={
    name:string;
    aVal:float;
    dType:DamType;
    info:string;
    iSpace:int;
    hidden:bool;
}

type Weapon={
    name:string;
    dVal:float;
    dType:DamType;
    info:string;
    iSpace:int;
    hidden:bool;
}
问题是,Gitem将怪物作为一种潜在类型,但是怪物可以将炸弹、武器或盔甲作为他们的地点,因此我创建了类型对象。 在我的代码后面,我写道:

match monster.sItem with  //gives them the item if the monster drops one
                    | Bomb bomb ->
                        procBomb bomb 

                    | Weapon weap -> 
                        procWeap weap

                    | Potion pot ->
                        procPot pot

                    | Armour arm ->
                        procArm arm
我得到的错误是表达式应该是Gitem,而不是obj,但是我不能将monster sitem字段设置为Gitem,这是不允许的,因为Gitem使用monster作为可能的类型。我试着制作另一种类型,比如Gitem,它拥有除monster之外的所有类型,但是这也会导致问题。
我是大学二年级学生,如果有任何帮助,我将不胜感激。

可以使用
关键字声明相互递归类型:

type A = { b : B }
and B = { a : A option }
此外,F#4.1引入了(和名称空间),它允许在模块(名称空间)级别上相互递归类型。如果您有许多相互依赖的冗长类型(请注意,没有
),这将非常有用:


可以使用
关键字声明相互递归类型:

type A = { b : B }
and B = { a : A option }
此外,F#4.1引入了(和名称空间),它允许在模块(名称空间)级别上相互递归类型。如果您有许多相互依赖的冗长类型(请注意,没有
),这将非常有用:


我试图理解这一点,所以我会在and关键字之后声明类型Monster,定义类型Gitem?这将允许我将Gitem作为Monster中的一个字段?您可以按任意顺序声明它们。唯一的要求是第二个得到的是
关键字,而不是
类型
@DanielPeedah是的,正如你所说的。我试图理解这一点,所以我会在和关键字之后声明类型Monster,定义类型Gitem?这将允许我将Gitem作为Monster中的字段?您可以按任意顺序声明它们。唯一的要求是第二个获得
关键字,而不是
类型
@DanielPeedah Yes,就像你说的那样。