F#核心库有个不起眼的函数id
,它非常简单,位于模块module FSharp.Core.Operators
,签名是:
val id: 't->'t
The identity function
更具体的实现代码是:
let id<'t> (x:'t) = x
I sometimes use id<_>
as a convenient way to do type annotation as prefix or without parens. It's a bit weird but it works nicely:
type Point = {x:int;y:int}
type Vector = {x:int;y:int}
let p = id<Point> {x=1;y=2}