小编k1u*_*k1u的帖子

F#中的区分联合类型检查具有不同的返回类型

我想在F#写一个翻译.我想检查表达式的类型.

这是我对表达式的歧视联盟

type Expr =
| Integer of int
| String of string
| Boolean of bool
Run Code Online (Sandbox Code Playgroud)

这是我用来检查类型的方法

let checkType (e:Expr) =
match e with 
| String s -> s
| Integer i -> i
| Boolean b -> b
Run Code Online (Sandbox Code Playgroud)

我希望方法确定表达式是字符串,整数还是布尔值.

但是,visual studio在checkType方法的第4行给出了以下错误:

 This expression was expected to have type string but here has type int
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?

f# interpreter functional-programming visual-studio discriminated-union

3
推荐指数
1
解决办法
632
查看次数