
传送门: https://blog.golang.org/generics-next-step
官方示例:
package main import ( "fmt" ) // The playground now supports parentheses or square brackets (only one at // a time) for generic type and function declarations and instantiations. // By default, parentheses are expected. To switch to square brackets, // the first generic declaration in the source must use square brackets. func Print[type T](s []T) { for _, v := range s { fmt.Print(v) } } func main() { Print([]string{"Hello, ", "playground\n"}) } High Level Overview:
1 cmdOptionKana 2020-08-04 19:20:23 +08:00 盲猜因为初始方案是小括号,被说小括号太多。而小于号大于号本质上不是括号,可能会影响编译速度(或语法分析的复杂性),官方好像一开始就讨厌尖括号。所以折中一下就是中括号了。 |
2 TypeError 2020-08-04 19:24:04 +08:00 看起来还好,能和圆括号分清就行 |
3 mind3x 2020-08-04 19:29:09 +08:00 很好,迈出了走向 Scala 的第一步 [并没有] |
4 janxin 2020-08-04 19:31:35 +08:00 因为不想用<>,因为不少额外有工作量,()有语法歧义 |
5 Fitz 2020-08-04 19:34:45 +08:00 唉, 什么时候改成方括号了, 记得 6 月份的时候不是圆括号吗 |
6 so1n 2020-08-04 19:35:38 +08:00 via Android 好像跟 python 不用<>的理由一样 |
7 Fitz 2020-08-04 19:38:21 +08:00 6 月份的帖子还在呢 https://v2ex.com/t/682238#r_9129287 |
9 aloxaf 2020-08-04 20:36:09 +08:00 看着比小括号舒服多了…… |
10 allenhu 2020-08-04 21:33:40 +08:00 via Android 恕我一眼看不懂 |
11 iyear 2020-08-04 21:36:39 +08:00 比尖尖的舒服多了。。。 |
12 zhuangzhuang1988 2020-08-04 21:52:52 +08:00 Scala 也是[] <> 是留给 xml 用的 |
13 tolerance 2020-08-04 22:45:52 +08:00 等正式发布再说 |
14 someonedeng 2020-08-05 09:22:39 +08:00 其实看起来还不错 |
15 yrj 2020-08-05 09:33:39 +08:00 via iPad 我记得上一稿方案是小括号,还是中括号舒服 |
16 sunxiansong 2020-08-05 10:43:36 +08:00 |
17 fengjianxinghun 2020-08-05 10:48:24 +08:00 欢迎,[]()()() 比 ()()()()至少强了一分钱。 |
18 lithbitren 2020-08-05 11:08:04 +08:00 比小括号要更好 |
19 fengjianxinghun 2020-08-05 11:11:56 +08:00 ```go package main import ( "fmt" ) // The playground now supports parentheses or square brackets (only one at // a time) for generic type and function declarations and instantiations. // By default, parentheses are expected. To switch to square brackets, // the first generic declaration in the source must use square brackets. type primitive interface { type string, int, uint, float32 } func Print[type T primitive](s []T) { for _, v := range s { fmt.Print(v) } fmt.Print("\n") } func main() { Print([]string{"Hello, ", "playground"}) Print([]int{1,2,3,4,5}) Print([]float32{1.0}) } ``` |
20 Mohanson 2020-08-05 13:20:39 +08:00 Golang 语法树解析的复杂度是 LR(1), 得益于其 Token 的 parser 是 context free 的. 按照 Go 的性格来说, 它不可能使用 <> , 因为一旦加上这个符号就会出现歧义, 要联系上下文去"猜"这个 Token 的含义, 复杂度会变成 LR(无穷). 括号内的数字和上下文的大小成正比. 编译速度在 Golang 看来是很重要的. |
21 fengjianxinghun 2020-08-05 15:09:23 +08:00 @Mohanson 这点前端 parse 的开销基本微乎其微。 |
22 Sasasu 2020-08-05 18:35:13 +08:00 LR(1) 并不是复杂度 |