【发布时间】:2017-04-05 22:24:02
【问题描述】:
我正在尝试将有区别的联合转换为字符串,但我不明白为什么这段代码不起作用。
type 'a sampleType =
| A of 'a
| B of 'a
let sampleTypeToString x =
match x with
| A (value) -> string value
| B (value) -> string value
这是 fsharp 交互式输出
sampleTypeToString A(2);;
Stopped due to error
System.Exception: Operation could not be completed due to earlier error
Successive arguments should be separated by spaces or tupled, and arguments involving function or method applications should be parenthesized at 3,19
This expression was expected to have type
'obj'
but here has type
'int' at 3,21
【问题讨论】:
-
你需要
box这个:sampleTypeToString (A(box(6)))。 -
这是使这个特定示例工作的快捷方式,但您必须同意这不是预期的。
-
不,这就是为什么它只是一个评论:) 但我想知道这是否可以在没有内联的情况下完成。
-
是的,可以。我的回答有两个选项。
标签: f# f#-interactive discriminated-union