Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Types F#相当于C#中的'is'关键字?_Types_F#_Keyword_Language Comparisons - Fatal编程技术网

Types F#相当于C#中的'is'关键字?

Types F#相当于C#中的'is'关键字?,types,f#,keyword,language-comparisons,Types,F#,Keyword,Language Comparisons,我的第一天。如果我有这个: let cat = Animal() 现在,我如何在后期检查猫是否是动物 在C中# 在F#?中,仅用于演示(不要定义is函数): let is//true @ildjarn在这里首先回答这个问题是值得称赞的,但是我在这里提交了答案,这样就可以接受了 C#的F#等价物是关键字是:?。例如: let cat = Animal() if cat :? Animal then printfn "cat is an animal." else printfn

我的第一天。如果我有这个:

let cat = Animal()
现在,我如何在后期检查猫是否是动物

在C中#

在F#?

中,仅用于演示(不要定义
is
函数):

let is//true
@ildjarn在这里首先回答这个问题是值得称赞的,但是我在这里提交了答案,这样就可以接受了

C#
的F#等价物是
关键字是:?。例如:

let cat = Animal()
if cat :? Animal then
    printfn "cat is an animal."
else
    printfn "cat is not an animal."

我知道我迟到了。如果尝试在fsi中测试集合的类型,请使用:? 如果项目类型不匹配,它将给出一个错误。例如

let squares = seq { for x in 1 .. 15 -> x * x }  
squares :? list<int> ;;   // will give false  
squares :? list<string> ;; // error FS0193: Type constraint mismatch
let squares=seq{for x in 1..15->x*x}
正方形:?列表;;//会给假
正方形:?列表;;//错误FS0193:类型约束不匹配

用Daniels这样的函数包装是
let b=cat:?Animal
@ildjarn你应该把它作为回答而不是评论,这样它才可以被接受。@Jack:在手机上,所以我不能给出一个完整的答案。请随意。:-]@ildjarn谢谢,我知道它与
有关:?
,但语法不正确!你也可以在类型上进行模式匹配。检查其他SO问题[此处][1]。[1] :丹尼尔,你能告诉我什么是
|>
?为什么要在函数调用之前包含它?它有名字吗?它叫向前管道操作员。它将左侧的操作数作为最后一个参数应用于右侧的函数。()丹尼尔,你为什么不把is定义为一个函数,对我来说很有用?@sacha:既然有一个内置的类型测试操作符(
:?
),你是对的。它需要翻译,即将
:?
读作“是”-与将
读作“大于”大致相同
let cat = Animal()
if cat :? Animal then
    printfn "cat is an animal."
else
    printfn "cat is not an animal."
let squares = seq { for x in 1 .. 15 -> x * x }  
squares :? list<int> ;;   // will give false  
squares :? list<string> ;; // error FS0193: Type constraint mismatch