naw*_*fal 13 f# types language-comparisons keyword
我的第一个F#日.如果我有这个:
let cat = Animal()
Run Code Online (Sandbox Code Playgroud)
现在我如何在以后检查cat is Animal?
在C#中
bool b = cat is Animal;
Run Code Online (Sandbox Code Playgroud)
在F#?
Jac*_* P. 25
@ildjarn值得在这里回答,但我在这里提交的答案是可以接受的.
C#is关键字的F#等价物是:?.例如:
let cat = Animal()
if cat :? Animal then
printfn "cat is an animal."
else
printfn "cat is not an animal."
Run Code Online (Sandbox Code Playgroud)
仅用于演示(不定义is函数):
let is<'T> (x: obj) = x :? 'T
type Animal() = class end
type Cat() = inherit Animal()
let cat = Cat()
cat |> is<Animal> //true
Run Code Online (Sandbox Code Playgroud)