Personally ()
parentheses seems like to be harder to read, too similar with function calls.
It would be nicer if we use brackets instead []
like in Nim, since []
only used in array/slice/indexing (index key or empty), which is more readable than ()
that used in many ways (receiver, parameter, return value, function calls, grouping, etc).
Current https://go2goplay.golang.org/p/zBO9K4-yXck
package main
import (
"fmt"
)
type Stack(type T) struct {
list []T
}
type Pair(type K, V) struct {
Key K
Val V
}
func New(type T)() Stack(T) {
return Stack(T){list: []T{}}
}
func (s *Stack(T)) Push(v T) {
s.list = append(s.list, v)
}
func main() {
a := New(int)()
fmt.Printf("%#v\n",a)
b := Stack(Stack(int)){}
fmt.Printf("%#v\n",b)
c := Pair(string,int){}
fmt.Printf("%#v\n",c)
}
Probably more readable syntax:
F:T
for single generic declaration and usage, eg. Stack:int
, BinaryTree:string
, Vector:Person
F:[T1,T2]
for multiple generic declaration and usage or when having constraint or array/slice, eg. Pair:[string,int]
, HashTable:[string,int]
, Stack:[T Stringer]
, Stack:[[3]int]
F:[T1:T2]
for nested generic usage, eg. Stack:[Queue:int]
So for example, if we want to use Stack that stores string-int Pair, we could use: Stack:[Pair:[string,int]]
, if we want to use Pair with string key and integer Stack value, we could use: Pair:[string,Stack:int]
The pros:
:
or :[]
symbol, other than just checking whether passed parameter is a datatype or variable/constant identifier. = unambiguoustype Stack:T struct {
list []T
}
type Pair:[K,V] struct {
Key K
Val V
}
func New:T() Stack:T {
return Stack:T{list: []T{}}
}
func (s *Stack:T) Push(v T) {
s.list = append(s.list, v)
}
func main() {
a := New:int()
fmt.Prinf("%#v\n",a)
b := Stack:[Stack:int]{}
fmt.Printf("%#v\n",b)
c := Pair:[string,int]{}
fmt.Printf("%#v\n",c)
}
Personally
()
parentheses seems like to be harder to read, too similar with function calls.