• 请不要在回答技术问题时复制粘贴 AI 生成的内容
viktor123
V2EX  ›  程序员

Golang 泛型:[type T],为啥要用中括号呢。。。

  •  1
     
  •   viktor123 · Aug 4, 2020 · 5123 views
    This topic created in 2284 days ago, the information mentioned may be changed or developed.

    传送门: 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:

    • Functions can have an additional type parameter list introduced by the keyword type: func F(type T)(p T) { ... }.
    • These type parameters can be used by the regular parameters and in the function body.
    • Types can also have a type parameter list: type M(type T) []T.
    • Each type parameter can have an optional type constraint: func F(type T Constraint)(p T) { ... }
    • Type constraints are interface types.
    • Interface types used as type constraints can have a list of predeclared types; only types whose underlying type is - one of those types can implement the interface.
    • Using a generic function or type requires passing type arguments.
    • Type inference permits omitting the type arguments in common cases.
    • If a type parameter has a type constraint its type argument must implement the interface.
    • Generic functions may only use operations permitted by the type constraint.
    22 replies  •  2020-08-05 18:35:13 +08:00
    QingXuJiaZhi
    QingXuJiaZhi
    Aug 4, 2020
    1
    盲猜因为初始方案是小括号,被说小括号太多。而小于号大于号本质上不是括号,可能会影响编译速度(或语法分析的复杂性),官方好像一开始就讨厌尖括号。所以折中一下就是中括号了。
    TypeError
    TypeError
    Aug 4, 2020
    2
    看起来还好,能和圆括号分清就行
    mind3x
    mind3x
    Aug 4, 2020❤️1
    3
    很好,迈出了走向 Scala 的第一步 [并没有]
    janxin
    janxin
    Aug 4, 2020
    4
    因为不想用<>,因为不少额外有工作量,()有语法歧义
    Fitz
    Fitz
    Aug 4, 2020
    5
    唉, 什么时候改成方括号了, 记得 6 月份的时候不是圆括号吗
    so1n
    so1n
    Aug 4, 2020 via Android
    6
    好像跟 python 不用<>的理由一样
    Fitz
    Fitz
    Aug 4, 2020
    7
    6 月份的帖子还在呢 https://v2ex.com/t/682238#r_9129287
    viktor123
    viktor123
    OP
    Aug 4, 2020
    8
    @Fitz thx bro.
    aloxaf
    aloxaf
    Aug 4, 2020
    9
    看着比小括号舒服多了……
    allenhu
    allenhu
    Aug 4, 2020 via Android
    10
    恕我一眼看不懂
    iyear
    iyear
    Aug 4, 2020
    11
    比尖尖的舒服多了。。。
    zhuangzhuang1988
    zhuangzhuang1988
    Aug 4, 2020
    12
    Scala 也是[]
    <> 是留给 xml 用的
    tolerance
    tolerance
    Aug 4, 2020
    13
    等正式发布再说
    someonedeng
    someonedeng
    Aug 5, 2020
    14
    其实看起来还不错
    yrj
    yrj
    Aug 5, 2020 via iPad
    15
    我记得上一稿方案是小括号,还是中括号舒服
    fengjianxinghun
    fengjianxinghun
    Aug 5, 2020
    17
    欢迎,[]()()() 比 ()()()()至少强了一分钱。
    lithbitren
    lithbitren
    Aug 5, 2020
    18
    比小括号要更好
    fengjianxinghun
    fengjianxinghun
    Aug 5, 2020
    19
    ```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})
    }


    ```
    Mohanson
    Mohanson
    Aug 5, 2020❤️1
    20
    Golang 语法树解析的复杂度是 LR(1), 得益于其 Token 的 parser 是 context free 的. 按照 Go 的性格来说, 它不可能使用 <> , 因为一旦加上这个符号就会出现歧义, 要联系上下文去"猜"这个 Token 的含义, 复杂度会变成 LR(无穷). 括号内的数字和上下文的大小成正比.

    编译速度在 Golang 看来是很重要的.
    fengjianxinghun
    fengjianxinghun
    Aug 5, 2020
    21
    @Mohanson 这点前端 parse 的开销基本微乎其微。
    Sasasu
    Sasasu
    Aug 5, 2020
    22
    LR(1) 并不是复杂度
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1338 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 58ms · UTC 22:25 · PVG 06:25 · LAX 14:25 · JFK 17:25
    ♥ Do have faith in what you're doing.