Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go 2 generic syntax readability: replace () with [] #39684

Closed
kokizzu opened this issue Jun 18, 2020 · 3 comments
Closed

Go 2 generic syntax readability: replace () with [] #39684

kokizzu opened this issue Jun 18, 2020 · 3 comments

Comments

@kokizzu
Copy link

kokizzu commented Jun 18, 2020

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:

  • 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)
}
@kokizzu kokizzu changed the title Go 2 generic syntax, replace () with [] Go 2 generic syntax readability: replace () with [] Jun 18, 2020
@mvdan
Copy link
Member

mvdan commented Jun 18, 2020

This is very explicitly discarded in the draft: https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-type-parameters.md#why-not-use-the-syntax

If you want to contest that conclusion, you should directly address that part of the draft, after having read the entire draft carefully.

@mvdan
Copy link
Member

mvdan commented Jun 18, 2020

Also, from the blog post:

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.

The issue tracker is only for go2go bugs.

@mvdan mvdan closed this as completed Jun 18, 2020
@kokizzu
Copy link
Author

kokizzu commented Jun 18, 2020

but this is different, :[] not just []
ok will use mailing list instead

https://groups.google.com/forum/#!topic/golang-nuts/zGQq_I5r2jg

polling: https://www.surveylegend.com/s/2dwe

@golang golang locked and limited conversation to collaborators Jun 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants