我试图为(!)运算符定义更一般的情况,如下所示,
let inline (!) (cell : ^a) =
(^a : (member Value : ^b) cell)
Run Code Online (Sandbox Code Playgroud)
因此它不仅适用于ref类型,而且适用于具有Value成员的任何类型.
> !(ref 10) ;;
val it : int = 10
> !(lazy 5) ;;
val it : int = 5
Run Code Online (Sandbox Code Playgroud)
但是当我尝试将它应用于具有类型变量的类型时会出现问题,
> let getValue (c : 'a ref) = !c ;;
let getValue (c : 'a ref) = !c ;;
------------------^^
C:\Users\User\AppData\Local\Temp\stdin(6,19): warning FS0064: This construct causes code to be less generic than indicated by the type annotations. The type …Run Code Online (Sandbox Code Playgroud)