如何检查函数的类型参数是否静态解析? [英] How to check if a function's type parameters are statically resolved?

查看:70
本文介绍了如何检查函数的类型参数是否静态解析?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常简单的示例:

  1. let myfun x = x
    这里在智能感知中说"x:'a->'a".在FSI中,它显示为"x:'a->'a"
  2. let inline myfun x = x
    这里在智能感知中说"x:'a->'a".在FSI中,它说"x:'a->'a"<<<<为什么不^a?
  3. let inline myfun (x: 'b) = x
    这里在智能感知中说"x:'b->'b".在FSI中,它显示为"x:'b->'b"
  4. let inline myfun (x: ^b) = x
    这里在智能感知中说"x:'b->'b".在FSI中,它表示"x:^ b-> ^ b"不同
  1. let myfun x = x
    Here in the intellisense it says "x: 'a -> 'a". In the FSI it says "x: 'a -> 'a"
  2. 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?
  3. let inline myfun (x: 'b) = x
    Here in the intellisense it says "x: 'b -> 'b". In the FSI it says "x: 'b -> 'b"
  4. let inline myfun (x: ^b) = x
    Here in the intellisense it says "x: 'b -> 'b". In the FSI it says "x: ^b -> ^b" <<<< different

既然智能感知器从不显示^b,那么我应该寻找^b作为FSI中静态解析"的指标吗?

Since the intellisense never shows ^b, should I look for ^b as an indicator of "statically resolved" in FSI?

inline是否保证静态解析"?

Does inline guarantee "statically resolved"?

推荐答案

内联确实允许但不强制使用静态解析类型,这就是为什么在情况2中它与情况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 ^.

例如,如果在情况3中将函数主体更改为sqrt x,则会得到

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.

这篇关于如何检查函数的类型参数是否静态解析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆