Go的类型推断算法

huangapple go评论85阅读模式
英文:

Go's Type Inference Algorithm

问题

Go编译器使用什么类型推断算法?

英文:

What type inference algorithm does the Go compiler use?

I tried looking this up on golang but I can't find documentation. I am tempted to assume that it would be Hindley-Milner, but I would like to know for sure

答案1

得分: 23

Go确实不使用Hindley-Milner。你为什么会这样认为呢?事实上,Go一般没有类型推断,只有在使用:=构造时才有,它使用了非常简单的规则,即将右侧的求值类型应用于左侧新声明的变量。实际上,这与C++11的auto关键字非常相似(除了对处理const和引用的规则)。

英文:

Go certainly doesn't use Hindley-Milner. Why would you think that? In fact, Go doesn't have type inference in general, only with the := construct, and that uses the extremely simple rule of taking the evaluated type of the right-hand side and applying it to the newly-declared variable on the left. It's actually pretty darn similar to C++11's auto keyword (except without the rules on handling const and references).

huangapple
  • 本文由 发表于 2012年9月26日 02:48:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/12589255.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定