module Mithra =
let Empty<‘T> = Seq.empty <‘T>
但是,这只是个语法糖而已。把这 F# 代码块编译成一个库,在C#里调用,即可看到,没有 Empty 变量,只有 Empty 函数。也就是说,这个“Empty”在 F# 里面,只是看起来像变量,实际上是在执行一个函数。// 声明泛型函数变量
let _getBytes<‘T> = ref Unchecked.defaultof< ‘T->byte[] >
// 泛型变量具现化,赋值
_getBytes<int16> := BitConverter.GetBytes
_getBytes<int32> := BitConverter.GetBytes
_getBytes<int64> := BitConverter.GetBytes
_getBytes<String> := Encoding.UTF8.GetBytes
// 取出函数
let GetBytes<‘T> = !_getBytes<‘T>
module Mithra =
type Cache<‘T> () =
static memberval GetBytes: ‘T -> byte[] = Unchecked.defaultof< ‘T ->byte[] > with get, set
Cache<int16>.GetBytes <- BitConverter.GetBytes
Cache<int32>.GetBytes <- BitConverter.GetBytes
Cache<int64>.GetBytes <- BitConverter.GetBytes
Cache<String>.GetBytes <- Encoding.UTF8.GetBytes
let GetBytes<‘T> = Cache<‘T>.GetBytes
原文:http://www.cnblogs.com/greatim/p/3943358.html