Yarn

Recent twts in reply to #uzv4seq

@prologic@twtxt.net That’s a cool concept! I’d love to have that at work, too.

When introducing generic functions you switched the parameter s to be the slice and not the element to search for. Maybe keep it consistent with the rest of the code samples and change s and x to xs and s. Or even better, use nicer names. :-)

Also, you might want to tell a few sentences about the ~. When I first encountered underlying types I was a bit puzzled.

Keep up the great Go talks!

​ Read More

So. Some bits.

i := fIndex(xs, 5.6)

Can also be

i := Index(xs, 5.6)

The compiler can infer the type automatically. Looks like you mention that later.

Also the infer is super smart.. You can define functions that take functions with generic types in the arguments. This can be useful for a generic value mapper for a repository

func Map[U,V any](rows []U, fn func(U) V) []V {
  out := make([]V, len(rows))
  for i := range rows { out = fn(rows[i]) }
  return out
}


rows := []int{1,2,3}
out := Map(rows, func(v int) uint64 { return uint64(v) })

I am pretty sure the type parameters goes the other way with the type name first and constraint second.

func Foo[comparable T](xs T, s T) int

Should be


func Foo[T comparable](xs T, s T) int

​ Read More

Participate

Login to join in on this yarn.