F# equivalent of `is` keyword in C#?

前端 未结 3 1589
心在旅途
心在旅途 2020-12-10 12:18

My first F# day. If I have this:

let cat = Animal()

Now how do I check at later stage if cat is Animal

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-10 12:28

    I know I'm late. If you try to test the type of a collection in fsi with :? it will give an error, if the item types do not match. E.g.

    let squares = seq { for x in 1 .. 15 -> x * x }  
    squares :? list ;;   // will give false  
    squares :? list ;; // error FS0193: Type constraint mismatch
    

    Wrapping in a function like Daniels is<'T> works.

提交回复
热议问题