Live data from Hacker News

Go 1.27 Interactive Tour

victoriametrics.com

81–90 of 219 posts

Re: Go 1.27 Interactive Tour

#81
post #29

Tried running a couple of examples in the tour, but ran into a few errors.

Tried reading your comment but ran into a lack of usable information.

If you try a new thing and run into 1 (or maybe 2 errors), maybe it's worth the time to report them.

If you run into a few errors, it's into "this isn't ready" or "they didn't try hard enough" territory, and it's not worth reporting the problems.

Re: Go 1.27 Interactive Tour

#84
One thing I think generics in Go is missing is the concept in Java.

If you're taking a List[T] and all you want to do is to call list.size() then you don't care what type of list it is. In Java you can write a function which takes a List but in Go you have to write List[T] so then the question becomes what is T? You have to make the function (or type you're a method on) generic. If you make the type generic then every user of your type also needs to specify T, etc.

I don't think it would be impossible to add that to Go. Allow List[?], which matches a List with any type parameter. Calling functions which don't involve the type parameter like list.size() would be fine, calling a method returning the type parameter like list.get(n) would return "any", and methods taking the type parameter like list.set(n, obj) would probably not be callable.

Re: Go 1.27 Interactive Tour

#85

This: "(b Box[T]) Map[U any](f func(T) U) Box[U]" is the type of cognitive weight I was happy that Go avoided.

I never understood the convention of using single letter names for generic parameters. I guess this started in C++ and every language has copied that convention. I think that code would be a lot easier to read if the types were called IN and OUT or In and Out or TIn and TOut or something like that.

I often use whole word for type annotation, when I can find meaningfull ones. I just type them in all caps to stay close to the convention.

I guess the single letter thing is laziness for a part. It's not simple to find words that represent the abstract idea behind the generic type without narrowing the possibilities. For array function, the Key Value from the sibling comment work but for more complex use case, it get complicated.

Re: Go 1.27 Interactive Tour

#87
post #80

Earlier quoted context omitted.

I never understood the convention of using single letter names for generic parameters. I guess this started in C++ and every language has copied that convention. I think that code would be a lot easier to read if the types were called IN and OUT or In and Out or TIn and TOut or something like that.

I believe Haskell did that for decades before C++.

Haskell was created in 1990, five years after C++.

Re: Go 1.27 Interactive Tour

#88

This: "(b Box[T]) Map[U any](f func(T) U) Box[U]" is the type of cognitive weight I was happy that Go avoided.

> (b Box[T]) Map[U any](f func(T) U) Box[U]

  Map method 
    of b (of type Box[T]) 
      that takes f 
      (of type function that takes value of type T and returns value of type U (which could be any type))
      and returns value of type Box[U]
  is defined as follows
    return Box[U]{v: f(b.v)}

  func[U any] b:Box[T].Map(f:func(T)->U)->Box[U]:
    return {v: f(b.v)}

  func[U any] Box[T].Map(f:func(T)->U)->Box[U]:
    return {v: f(this.v)}

  // maybe all of the types could be inferred from usage?
  func Box[].Map(f):
    return Box[]{v: f(this.v)}
Eh... I think you'd need to avoid generics altogether.

Re: Go 1.27 Interactive Tour

#89

This: "(b Box[T]) Map[U any](f func(T) U) Box[U]" is the type of cognitive weight I was happy that Go avoided.

It's hard to avoid, because (naming aside) the cognitive load is caused by higher order functions, which are hard avoid without causing massive code duplication. I understand the desire to keep things concrete and avoid high level abstractions, but it's a decision not to automate stuff that can easily be automated. It runs counter to the basic instincts and purpose of our field/industry. That's why it never sticks.

Honestly, I’ve written some applications that, on paper, should be the perfect candidate for generics. And yet I can still count on one hand the number of times generics have saved me from massive code duplication.

Most of the time generics might be useful, I’ve ended up needing reflection too anyway. And at that point, I’m really no better off for generics.

Post reply on HN