2016-09-12 121 views
1

我想檢查一個變量是某種類型的,像這樣:F#爲什麼我不能使用:? F#交互式操作符?

let s = "abc" 
let isString = s :? string 

但在F#互動,我得到以下錯誤:

error FS0016: The type 'string' does not have any proper subtypes and cannot be used as the source of a type test or runtime coercion.

這究竟是爲什麼?我期望isString是一個布爾。

+3

這是因爲你正在嘗試密封類型。 – Gustavo

回答

7

因爲您嘗試使用密封類型。

嘗試,而不是這樣的:

let s = box "abc" 
let isString = s :? string 

有在做這個測試的脅迫與密封的類型,因爲他們不能有任何亞型,這就是錯誤消息告訴你沒有任何意義。