【发布时间】:2014-12-08 15:51:41
【问题描述】:
我正在学习如何在 F# 中创建复合*类型,但遇到了一个问题。我有这种类型和一个 ToString 覆盖。
type MyType =
| Bool of bool
| Int of int
| Str of string
with override this.ToString() =
match this with
| Bool -> if this then "I'm True" else "I'm False"
| Int -> base.ToString()
| Str -> this
let c = Bool(false)
printfn "%A" c
我在 ToString 覆盖中收到一个错误,提示“此构造函数应用于 0 个参数,但需要 1 个”。我很确定这段代码不会编译,但它显示了我正在尝试做的事情。当我注释掉覆盖并运行代码时,c 打印为“val c : MyType = Bool false”。当我进入该代码时,我看到 c 有一个属性 Item 设置为布尔值 false。但是,我似乎无法在代码中访问此属性。即使我注释c也不会。
在这种情况下我应该如何覆盖 ToString?
* 我很确定这些被称为复合类型。
【问题讨论】:
标签: f# overriding composite-types