问题
very simple examples:
let myfun x = x
Here in the intellisense it says "x: 'a -> 'a". In the FSI it says "x: 'a -> 'a"let inline myfun x = x
Here in the intellisense it says "x: 'a -> 'a". In the FSI it says "x: 'a -> 'a" <<<< why not^a
?let inline myfun (x: 'b) = x
Here in the intellisense it says "x: 'b -> 'b". In the FSI it says "x: 'b -> 'b"let inline myfun (x: ^b) = x
Here in the intellisense it says "x: 'b -> 'b". In the FSI it says "x: ^b -> ^b" <<<< different
Since the intellisense never shows ^b
, should I look for ^b
as an indicator of "statically resolved" in FSI?
Does inline
guarantee "statically resolved"?
回答1:
Inline does allow but does not force statically resolved types, that's why in case 2. it remains the same as in case 1.
I think in most cases type inference is smart enough to guess if the type should really be statically resolved, even if you don't specify the ^
.
For example if you change your function body to sqrt x
in case 3. you'll get
> let inline myfun (x: 'b) = sqrt x;;
val inline myfun : ^b -> ^a when ^b : (static member Sqrt : ^b -> ^a)
I personally always try not to specify types explicitly at first try, then I check if I'm happy with the inference, if I'm not then I try adding inline, but not the hat types.
Why intellisense shows sometimes something different? that's probably a small bug.
来源:https://stackoverflow.com/questions/16327655/how-to-check-if-a-functions-type-parameters-are-statically-resolved