You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
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:
we could always differentiate between function calls (that returns function) and generics by : or :[] symbol, other than just checking whether passed parameter is a datatype or variable/constant identifier. = unambiguous
consistent syntax between generic declaration and usage
type 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)
}
The text was updated successfully, but these errors were encountered:
The best way to provide feedback for the language changes will be on the mailing list golang-nuts@googlegroups.com. Mailing lists are imperfect, but they seem like our best option for initial discussion. When writing about the design draft, please put [generics] at the start of the Subject line and to start different threads for different specific topics.
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
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. = unambiguousThe text was updated successfully, but these errors were encountered: