F# 静态解析变量类型

文 / 李博(光宇广贞)

       《方法多态与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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值