Live data from Hacker News

Why Generics?

blog.golang.org

181–190 of 261 posts

Re: Why Generics?

#181

Earlier quoted context omitted.

Inheritance makes generics difficult. For instance, if A If you are just reading the array, you would want Array[A] This problem doesn't come up in ML style languages because they do not make use of inheritence.

It comes up in Scala. The solution is to have notation to specify the variance of types.

That would be the crazy experimental weird feature.

Re: Why Generics?

#182

Earlier quoted context omitted.

Yes, though Scala can hardly be an example of simplicity.

I don't think it's the simplest language out there but its parametric polymorphism is quite straightforward

Once you figure out co- and contravariance, yes. But neurosurgery is straightforward, too. It's just getting from here to there.

Re: Why Generics?

#183

Maybe this is unfair, but it’d be great if the Go devs could just say “generics will work similarly to [C# / Java / Swift / D / whatever], except that we’ll address [problems] with [adjustments]”. Rather than going through this whole rigmarole of resisting adding generics too early because all existing implementations are bad, then slowly reinventing the wheel from scratch, then finally ending up with something prett…

What is your point? Serious question. If you tell me, for example, that Go generics are going to be like C# generics, then I have to be familiar with the full semantics of C# generics. Essentially you tell me that in order to understand X I have to understand Y first, that's not good. Consumers of your language are not, for the MOST part, PLT nerds.

The big advantage is that PLT nerds (Who else are you going to learn a programming language from?) already know the ugly, hairy bits of Y and how to work around them. The alternative is for X to invent new ugly, hairy bits.

Re: Why Generics?

#184
post #47
post #28

Chiming in here as a Swift dev - I find its generics system incredibly helpful and end up writing something that uses generics about once a month. To those Go programmers who think they will never use them - it’s worth a little learning, and once you do you will find more ways to use them to make your code more applicable.

Chiming in as a fervent Go dev that's also a huge fan of generics: Generics are awesome, but always seem to add a ton of complexity. Everyone starts with just "oh, `func Reverse(slice []T)` is so obvious" kind of thing, but that's like saying `fmt.Println("Hello World")` is simple. It's simple cause you are doing a simple thing. That said, I do want generics to come to Go, just... with an emphasis on what Go is, not…

Pettifoging. I don't care what people do with them.

I know how to use them. They are valuable to me.

Re: Why Generics?

#185
post #48

I think another area of Go that could use improvements is a more portable standard library optimised for embedded and OS dev. A lot of people want to use Go as a "systems language" and do drivers etc. in it. But the standard library don't have good support for freestanding, or at least it's not a priority. C and Rust and other languages designed for usage in a non-hosted environment have very clear delineated areas o…

"Go's runtime by default assumes the presence of an operating system." It also assumes the existence of several megabytes of runtime and enough RAM to feasibly use GC, which if nothing else is probably a real mess when it comes to drivers. What you probably need, rather than being a second-class citizen of the main Go language indefinitely, is a separate dialect that is close to Go, but can go its own way if it makes…

There is nothing wrong with GC. There exist plenty of network stacks in production that are written entirely in Go (See Docker/Kubernetes et alia). There are also operating systems that have GCs, such as Oberon or the few C# OSes written by MSFT and others. As long as care is taken to isolate hard real-time requirements when needed, a GC is perfectly fine. There is a real advantage to Rust's style of explicitly marking no_std. After all, what's the point of using stuff like tinygo (other than for performance/resource reasons) if it cannot run most of the existing Go libraries?

Re: Why Generics?

#186
post #138

Not certain I like that this is now valid Go: func Foo (type T) (t T) (t T, err error) { … } Seems like an awful lot of parentheses in a single line! It's a bit tricky to read, because 'generic function' here means something subtly different from what 'generic function' means in CLOS …

It's not more parentheses than in a method definition:

    func (a A) Foo(x int) (n int, err error) { … }
Also your line isn't formatted properly. It should be:

    func Foo(type T)(t T) (t T, err error) { … }

Re: Why Generics?

#187
post #91

Earlier quoted context omitted.

Must every popular language evolve into C++ or Common Lisp?

I wonder if there's room for a language that is small, allows for nearly limitless abstraction, and still has great tooling. Go is (or, you could argue, was) small, and now has better tooling, but is just beginning to increase its ability to create abstractions. Common Lisp is large (only 200 pages fewer in its spec than C++, if I remember correctly), has unparalleled tooling (like the don't-unwind-the-stack debuggin…

Scheme?

Re: Why Generics?

#188

Maybe this is unfair, but it’d be great if the Go devs could just say “generics will work similarly to [C# / Java / Swift / D / whatever], except that we’ll address [problems] with [adjustments]”. Rather than going through this whole rigmarole of resisting adding generics too early because all existing implementations are bad, then slowly reinventing the wheel from scratch, then finally ending up with something prett…

If you're just going to copy another language there really isn't much of a point in making a new language. Go is not java, or c# or rust.

Part of the explicit goal stated by the go team is that generics must still feel like go. If you slapped java generics onto go it would not feel like go.

Re: Why Generics?

#189
post #119

Earlier quoted context omitted.

You can tell if it’s a function call or a generic type based on where it is used; the local context. Function calls are either statements or expressions. Types appear in declarations.

a := Bar(baz)(buzz) Is Bar a `func(some) func(thing) other` or is it a `func(type T)(some) other`? You need to know what “baz” is to be able to tell.

Normally you wouldn't write that since the compiler will be able to infer the baz type based on buzz (assuming this is a call to a generic function).

Re: Why Generics?

#190
post #139
post #37

Earlier quoted context omitted.

Which interfaces-based solution? The one used by sort is a hack that only works for some of the things that you would want to do. The one where you use introspection performs badly and isn't typesafe. The tree implementation in the article cannot be done in a safe and performant way in Go today.

The one mentioned in the article. It's not a hack: it's a runtime equivalent that requires that your type implements some interface.

It is a hack. The article lists a whole series of things that are standard in other languages that you can't do in go. Here are some examples:

  Find smallest/largest element in slice
  Find average/standard deviation of slice
  Compute union/intersection of maps
  Find shortest path in node/edge graph
  Apply transformation function to slice/map, returning new slice/map
And here are data structures that other languages have but go does not:

  Sets
  Self-balancing trees, with efficient insertion and traversal in sorted order
  Multimaps, with multiple instances of a key
  Concurrent hash maps, supporting parallel insertions and lookups with no single lock
If it were not a hack, then go would not have these limitations.
Post reply on HN