Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I suppose this is necessary because this:

  func clone[S ~[]any)(s S) S
would only allow things with an underlying type of []interface{}, not "any type" as an inferred type... and that applies to the final example too:

  // allows any collection of stringable things
  func WithStrings[S ~[]E, E interface { String() string }](
  
  // allows only things like:
  // []interface { String() string }{...}
  // and named types like that, but not:
  // []strings.Builder{...}
  // because that isn't the same collection type,
  // it's just a collection of compatible elements
  func WithStrings[S ~[]interface { String() string }](...)
I guess this is the price to pay to avoid introducing co/contra variance? It may be worth it, and it seems likely that it would be a thing you can improve without breaking compatibility.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: