F#为什么我不能使用:? F#交互中的运算符? [英] F# Why can't I use the :? operator in F# interactive?

查看:95
本文介绍了F#为什么我不能使用:? F#交互中的运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to check if a variable is of a certain type like so:

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

但是在F#交互式中,出现以下错误:

but in F# interactive, I get the following error:

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会是个笨蛋.

Why is this happening? I expect isString to be a bool.

推荐答案

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

Because you are trying with a sealed type.

尝试以下方法:

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.

这篇关于F#为什么我不能使用:? F#交互中的运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆