我的问题与以下程序有关。
open System
// Types
type Car (brand: string) =
member _.Brand = brand
type BMW () =
inherit Car "BMW"
type Pet =
| Cat
| Dog
[<EntryPoint>]
let main argv =
// Match subtype of test-expression type: ok
let car = Car "Mercedes"
let carResult =
match car with
| :? BMW -> 1
| _ -> 0
// Match type of test-expression: "will always hold" warning
let bmw = BMW ()
let bmwResult =
match bmw with
| :? BMW -> 1
| _ -> 0
// Catch any exception: "will always hold" warning
let exceptionResult =
try
1/0
with
| :? Exception -> 2
// Match type of test-expression (union type): "The type 'Pet' does not have any proper subtypes" error
let cat = Cat // this has type Pet
let catResult =
match cat with
| :? Pet -> 1
| _ -> 0
0
在第一个测试中, test-expression 是 type Car
,其中类型测试模式中的类型BMW
, 是子类型,并且没有警告或错误。在第二个和第三个测试中,类型测试模式中的类型与测试表达式的类型相同,并且会引发警告,这是可以理解的,因为当程序员测试 aBMW
是否真的是 a BMW
,或者 anException
是真的一个Exception
,这很可能是一个逻辑错误。
最后一个测试与测试二和测试三具有相同的形式:测试表达式的类型是Pet
,类型测试模式也是Pet
。那么为什么在这种情况下会产生错误呢?错误说The type 'Pet' does not have any proper subtypes...
。但BMW
没有任何子类型,也不会产生此错误。此外,模式匹配页面(在“类型测试模式”下)说“如果输入类型与模式中指定的类型(或派生类型)匹配,则匹配成功。” Pet
是匹配Pet
,ergo 等。为什么联合类型的处理方式不同?
也许错误消息的措辞有点过于含糊。不是类型Pet
没有子类型,而是它不能有子类型。
因为BMW
是一个类,它可能有一个来自不同程序集的子类型,它是在BMW
定义自身的程序集之后编译的。
但是Pet
这不可能发生,因为 sum 类型不能有子类型,因此禁止对这些类型进行继承匹配。
还要注意,错误的来源是被匹配变量的类型,而不是模式的类型。因此,例如,这将编译而不会出错:
let n : obj = null
match n with
| :? Pet -> "is a pet"
| _ -> "no idea"
这是有效的,因为n
是 type obj
,它确实有子类型,其中Pet
之一(毕竟这是 .NET,一切都是对象)。但是匹配您的变量cat
不起作用,因为该变量具有固有的无子类型类型。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句