为什么没有Go的泛型

为什么没有Go的泛型

本文介绍了为什么没有Go的泛型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明:我现在只玩了一天Go,所以很有可能我错失了很多。



有人知道为什么会有没有真正的支持泛型/模板/ WhatsInAName在Go?所以有一个通用的 map ,但这是由编译器提供的,而Go程序员不能编写自己的实现。所有关于使Go尽可能正交的讨论,为什么我可以使用泛型类型但不创建新类型?



特别是当涉及函数式编程时,那里是lambdas,甚至是闭包,但是对于缺乏泛型的静态类型系统,我该如何编写泛型高阶函数,如 filter(predicate,list)? OK,链接列表等可以通过 interface {} 牺牲类型安全来完成。



在SO / Google上没有透露任何见解,它看起来像泛型一样,如果有的话,将作为事后补充加入Go。我确信汤普森比爪哇人做得更好,但为什么要把泛型放在外面?或者他们是否计划并且尚未实施? 解决方案

这个答案你会在这里找到:


Disclaimer: I've only played with Go for one day now, so there's a good chance I've missed a lot.

Does anybody know why there is no real support for generics/templates/whatsInAName in Go? So there is a generic map, but that's supplied by the compiler, while a Go programmer can't write her own implementation. With all the talk about making Go as orthogonal as possible, why can I USE a generic type but not CREATE a new one?

Especially when it comes to functional programming, there are lambdas, even closures, but with a static type system lacking generics, how do I write, well, generic higher order functions like filter(predicate, list)? OK, Linked lists and the like can be done with interface{} sacrificing type safety.

As a quick search on SO / Google did not reveal any insights, it looks like generics, if at all, will be added to Go as an afterthought. I do trust Thompson to do way better than the Java guys, but why keep generics out? Or are they planned and just not implemented yet?

解决方案

this answer you will find here: http://golang.org/doc/faq#generics

这篇关于为什么没有Go的泛型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:49