Skip to content

Instantly share code, notes, and snippets.

@peter-mckenzie
Last active September 6, 2018 19:41
Show Gist options
  • Save peter-mckenzie/5cc6530da1d966e743f4a39c150a6ac2 to your computer and use it in GitHub Desktop.
Save peter-mckenzie/5cc6530da1d966e743f4a39c150a6ac2 to your computer and use it in GitHub Desktop.
Alternative generics syntax

Here's an alternative Go generics syntax that uses the familiar <> but avoids parsing complexity by placing it to the left of the identifier. It's a bit (very?) unusual, but has the benefits of brevity and making the type parameters stand out.

Illustrated by converting some examples from Generics — Problem Overview :

type <T>List []T
func <K, V>Keys(m map[K]V) []K

var ints <int>List
keys := <int, string>Keys(map[int]string{1: "one", 2: "two"})

type <T Equal>Set []T
func (s <T>Set) Find(x T) int {
  ...
}
@peter-mckenzie
Copy link
Author

Well spotted thanks. Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment