我試圖在F#中編寫一個解釋器。我想檢查表達式的類型。在F#中使用不同的返回類型進行歧視的聯合類型檢查
這裏是我的表達
type Expr =
| Integer of int
| String of string
| Boolean of bool
這是我使用與
let checkType (e:Expr) =
match e with
| String s -> s
| Integer i -> i
| Boolean b -> b
我想要的方法來確定閹一個表達式來檢查類型的方法識別聯合是一個字符串,整數或布爾值。
然而,視覺工作室給我的checkType方法的4線以下錯誤:
This expression was expected to have type string but here has type int
我這麼想嗎?
當前的錯誤是由於一個事實,即函數只能返回一個類型的表達式 –