What will generics be like in Go?

Recently, there has been a lot of criticism about Go because it does not support generics. What exactly does this mean? How would you explain this to someone from a dynamically typed language like Ruby, where this is not a familiar concept?

+7
source share
2 answers

In a dynamically typed language, you don't care what type of list it is, it's just a list. However, in a statically typed language, you don't care what type of list it is, because the type is "list A", where "A" is some type. That is, list Adifferent from list B.

, , A -> B, foreach , list A. ... , , A, . , , list C A -> B, A === C . .

Go, , , int, double, .. t "" .

+7

. "" , Go, :

.
, , - , . , , :

type Hashable interface {
  Hash() []byte
}

func printHash(item Hashable) {
   fmt.Println(item.Hash())
}

Hashable printHash, . .

, ?
. Go:

( )

type LinkedList struct {
   value interface{}
   next *LinkedList
}

func (oldNode *LinkedList) prepend(value interface{}) *LinkedList {
   return &LinkedList{value, oldNode}
}

"" Go - , . Java, 2004 . , .

, , , . , :

node := tail(5).prepend("Hello").prepend([]byte{1,2,3,4}) 

, , , boileplate .

gen project :

gen , .
gen ; .


2017 : , Generics for Go " " " Simplicity Debt Redux".

Go 2.0 , , Generics, :

  • : Generic , : .
    : !
  • : {} .
    , .
  • . , , , io.Reader.Read?
    , ​​ , ?
  • : Gos Pascal , . , ?
  • Iterator: , , - . , - - , .
    , Iterable , , , , .
  • . const , , , , , Go.
    , , Go, - , .

Russ Cox " My Go Resolutions for 2017 ":

, , , .

15292: " " " .

+3

Source: https://habr.com/ru/post/1661364/


All Articles