【发布时间】:2011-01-18 17:30:35
【问题描述】:
在 Haskell 中,只需将 deriving Show 添加到类型定义中,就可以轻松地将代数类型/可区分联合“显示”为字符串。
在 F# 中,我最终会写出如下内容:
type Pos =
| Pos of int * int
override this.ToString() =
match this with
Pos(startp, endp) -> sprintf "Pos(%d, %d)" startp endp
显然,更复杂的类型会变得更糟。
在 F# 中获得类似 deriving Show 的任何方法?
【问题讨论】:
-
Err,F# 已经自动为您执行此操作,因此您无需编写
deriving Show。
标签: .net haskell f# discriminated-union object-to-string