Dan*_*iel 5 generics f# exception
我尝试了以下,但不起作用.
exception MyError<'a> of 'a
exception 'a MyError of 'a
Run Code Online (Sandbox Code Playgroud)
我必须使用长形式:
type MyError<'a>(value) =
inherit System.Exception()
member this.Value : 'a = value
Run Code Online (Sandbox Code Playgroud)
根据规范,您必须使用长格式.我没有找到任何解释为什么会这样,但异常声明的语法看起来像这样(也许还提示为什么行为如你所描述的那样):
exception-defn:= attributes opt exception union-type-case-data
联合型的病例数据:=
IDENT (无参工会情况)
IDENT 的类型*...*型 (n进制工会情况)
IDENT:uncurried-SIG (n进制工会情况下)
这非常有趣,因为它表明异常声明更像是有区别的联合案例而不是类似的类型.我想你可以想到一个异常声明......
exception MyExn of int
Run Code Online (Sandbox Code Playgroud)
...作为向标准System.Exception
类型添加新案例的声明(如果它是一个有区别的联盟).在这种情况下,您不希望能够使用泛型类型参数:
type System.Exception =
| ...
| MyExn of int
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
213 次 |
最近记录: |