繁体   English   中英

F#为什么我不能使用:? F#交互中的运算符?

[英]F# Why can't I use the :? operator in F# interactive?

我正在尝试检查变量是否具有某种特定类型,例如:

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是一个布尔。

因为您正在尝试使用密封类型。

尝试这样:

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

使用密封类型进行强制测试没有意义,因为它们不能具有任何子类型,这就是错误消息告诉您的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM