简体   繁体   English

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

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

I am trying to check if a variable is of a certain type like so: 我正在尝试检查变量是否具有某种特定类型,例如:

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

but in F# interactive, I get the following error: 但是在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.

Why is this happening? 为什么会这样呢? I expect isString to be a bool. 我期望isString是一个布尔。

Because you are trying with a sealed type. 因为您正在尝试使用密封类型。

Try instead this: 尝试这样:

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

There is no point in doing this coercion tests with sealed types since they can't have any subtype and that's what the error message is telling you. 使用密封类型进行强制测试没有意义,因为它们不能具有任何子类型,这就是错误消息告诉您的内容。

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

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