使用泛型类型签名覆盖方法
Override method with generic type signature
如何重写具有泛型类型签名的抽象方法,并在子类中为其赋予更具体的参数类型?
type Rule() =
abstract Core : 'T -> bool
default _.Core _ = false
type Entity = {
Name: string
State: string
}
type Wisconsin() =
inherit Rule()
override _.Core (entity: Entity) =
entity.State = "WI"
SimpleMemberOverload.fsx(256,22):错误 FS0001:此表达式应具有类型
''a'
但这里有类型
'Entity'
你不能。 毕竟,说一个规则需要 any 'T 然后有这样一个规则只接受实体会违约。具有指向类型 Wisconsin
的对象的类型 Rule
的引用的调用者应该期望什么,例如string
传递给 Core
?
但是您可以像这样定义各种规则:
type Rule<'T> () =
abstract Core : 'T -> bool
default _.Core _ = false
type Entity = {
Name: string
State: string
}
type Wisconsin() =
inherit Rule<Entity>()
override _.Core entity =
entity.State = "WI"
意味着 Wisconsin
不会缩小参数类型,但现在 是 规则 for/of 个实体。
如何重写具有泛型类型签名的抽象方法,并在子类中为其赋予更具体的参数类型?
type Rule() =
abstract Core : 'T -> bool
default _.Core _ = false
type Entity = {
Name: string
State: string
}
type Wisconsin() =
inherit Rule()
override _.Core (entity: Entity) =
entity.State = "WI"
SimpleMemberOverload.fsx(256,22):错误 FS0001:此表达式应具有类型 ''a' 但这里有类型 'Entity'
你不能。 毕竟,说一个规则需要 any 'T 然后有这样一个规则只接受实体会违约。具有指向类型 Wisconsin
的对象的类型 Rule
的引用的调用者应该期望什么,例如string
传递给 Core
?
但是您可以像这样定义各种规则:
type Rule<'T> () =
abstract Core : 'T -> bool
default _.Core _ = false
type Entity = {
Name: string
State: string
}
type Wisconsin() =
inherit Rule<Entity>()
override _.Core entity =
entity.State = "WI"
意味着 Wisconsin
不会缩小参数类型,但现在 是 规则 for/of 个实体。