正文

F# 静态解析变量类型2012-01-15 09:36:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/iamben250/53150.html

分享到:

http://blog.csdn.net/hikaliv/article/details/4565015 

文 / 李博(光宇广贞)

       《方法多态与Duck typing、C#之拙劣与F#之优雅》文末留了个尾巴,F# 的 inline 与 ^ 算符如何联袂完成方法多类的类型检查的。本文讨论这一问题。

       《F# 手册》§5.1.2 提到:

A type of the form ^ident is a statically resolved variable type. A fresh type inference variable is created and added to the type inference environment (see §14.6). This type variable is tagged with an attribute indicating it may not be generalized except at inline definitions (see §14.7), and likewise any type variable with which it is equated via a type inference equation may similarly not be generalized.

       问题解决了。那范型 'a 与 ^a 的区别呢?差就差在 ^a 需要 inline,见如下示例:

> let id (a : 'a) = a;; 
val id : 'a -> 'a 
> id 1, id "hi";; 
val it : int * string = (1, "hi") // Good, it's generic 
> let id (a : ^a) = a;; 
  let id (a : ^a) = a;; 
  -------------^^ 
stdin(8,14): warning FS0064: This construct causes code to be less generic than indicated by the type annotations. The type variable 'a has been constrained to be type 'obj'. 
val id : obj -> obj 
> id 1, id "hi";; 
val it : obj * obj = (1, "hi") // Constrained to obj - not generic 
> let inline id (a : ^a) = a;; 
val inline id :  ^a ->  ^a 
> id 1, id "hi";; 
val it : int * string = (1, "hi") // Since it's inline, it's generic

阅读(2259) | 评论(1)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册