如何覆盖以下代码中的Zero
方法,以便可以为Euro(0)
中的定义返回type Euro
[<AbstractClass>]
type Currency () =
abstract member Zero<'T when 'T :> Currency > : unit -> 'T
type Euro (value: int) =
inherit Currency()
member this.Value = value
override this.Zero() = Euro(0) :> _
最佳答案
您是否尝试过将通用约束提升到类级别?
[<AbstractClass>]
type Currency<'T when 'T :> Currency<'T>>() =
abstract member Zero : unit -> 'T
type Euro (value: int) =
inherit Currency<Euro>()
member this.Value = value
override this.Zero() = Euro(0)
尽管自引用泛型在我看来总是很奇怪,但这是在C#中完成的方式。
关于f# - 我如何正确重写一个受约束的方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42253991/