Earlier quoted context omitted.
As a C++ (including modern) developer for more than 20 years, I had written a "template" keyword only for a handful of times. Maybe once in every 5 years on average... :) Unless you are a compiler/stdlib vendor or contributing to Boost, there are features that you just don't use it daily.
But you would still have to deal with the mental load associated with having templates in your stack. I don't know if C++ is still as bad as it was back in my day about vomiting 20-line error messages at you because "std::vector " or something has a billion implied template arguments. That was definitely a cost that one had to bear even if one never used the "template" keyword.
Go 1.27 Interactive Tour
201–210 of 219 posts
Re: Go 1.27 Interactive Tour
#202Earlier quoted context omitted.
I’ve written a lot of libraries too. The problem is generics only solve a very small part of the equation: compile time checks for composite types. But to use composite types in anything non-trivial in Go, you then need reflection. Which is slow. And if you then need reflection, you’re already passing interface types anyway plus you’re back to having to handle type-handling errors in the runtime. So if you’re writing…
Yes it is precisely Go’s shortcomings that would require typical use of generics to also require reflection most of the time. You would want Rust traits (or Haskell type classes) or C++ style type traits and then the need for reflection is much reduced. So Go has painted itself into a corner where generics feel bolted on and less useful than generics in other languages. It’s still Go’s fault and people rightfully arg…
Go generics does have this feature. You can require a generic type to implement an interface.
Re: Go 1.27 Interactive Tour
#203Earlier quoted context omitted.
No. I kind of want to leave it there. But that will probably be looked on disfavorably. I've seen at least a dozen attempts. It's not like it's hard to write it out. There's maybe a couple of variants but they're all just a handful of lines. The problem is, once you have an Option in hand, you end up trading: val, err := whatever(...) if err != nil { // handle error } // use val for val := whatever(...) if err, isErr…
I think my opinion will be even more maligned: I like Java-style checked exceptions. It forces awareness of the error and a clean way to propagate it.
Still, to really make it work nicely is non-trivial in the presence of things like closures and I couldn't tell you right now how to fix it.
Re: Go 1.27 Interactive Tour
#204Earlier quoted context omitted.
No. I kind of want to leave it there. But that will probably be looked on disfavorably. I've seen at least a dozen attempts. It's not like it's hard to write it out. There's maybe a couple of variants but they're all just a handful of lines. The problem is, once you have an Option in hand, you end up trading: val, err := whatever(...) if err != nil { // handle error } // use val for val := whatever(...) if err, isErr…
If you get the ? from rust in addition to the option, then it’s suddenly a lot more convenient. It will do `if err != nil { return err; }` in a single symbol. There’s also convenience at the returning side. You can always just return the error, and not have to care about dummy values for the other return values (which is especially annoying when changing the returned types). That said, it might still not be worth the…
The minimal error handling code in Go is really:
if err != nil {
return fmt.Errorf("can't do thing thing I'm trying to do: %w", err)
}
That is actually the code we want to be made easier."if err != nil { return err }" in my code is actually a specific claim that for error-handling purposes this function is conceptually part of the function that is calling it and it has just been factored out for other reasons.
Re: Go 1.27 Interactive Tour
#205>The quieter but bigger change I really wish they didn't use such stupid LLM-isms.
I do wonder whether, as a group of people being regularly exposed to text written by LLMs, we'll gradually end up writing and talking like that in our normal language. At that point perhaps text written by LLM and human will be indistinguishable. I already find myself using terms like 'footgun' in jest more than I ever did before! "The creatures outside looked from pig to man, and from man to pig, and from pig to man…
Re: Go 1.27 Interactive Tour
#206Earlier quoted context omitted.
Right? Sigh. I really dislike this. There are 37000 programming languages, stop forcing every single one that gets popular to look like this.
It's almost as if the choices that make languages "look like this" are the ones that are generally associated with a more productive development workflow.
Re: Go 1.27 Interactive Tour
#207Earlier quoted context omitted.
Yes it is precisely Go’s shortcomings that would require typical use of generics to also require reflection most of the time. You would want Rust traits (or Haskell type classes) or C++ style type traits and then the need for reflection is much reduced. So Go has painted itself into a corner where generics feel bolted on and less useful than generics in other languages. It’s still Go’s fault and people rightfully arg…
>You would want Rust traits Go generics does have this feature. You can require a generic type to implement an interface.
It reminded me of early years of Rust where people saw traits and asked, well Java had interfaces for many decades, what’s the innovation?
Re: Go 1.27 Interactive Tour
#208Earlier quoted context omitted.
>You would want Rust traits Go generics does have this feature. You can require a generic type to implement an interface.
Requiring the implementation of a Go interface is less powerful than requiring a trait. It reminded me of early years of Rust where people saw traits and asked, well Java had interfaces for many decades, what’s the innovation?
You're right of course that traits are more powerful than interfaces. Go simply isn't designed to satisfy fans of elaborate type systems. This can be frustrating at times, but it also means that you don't have to deal with overarchitected library code like this: https://github.com/marshallpierce/rust-base64/issues/213 (Yes, you can overarchitect code in Go too, but the simplicity of the language, and the model of the stdlib, do tend to discourage this in practice.)
I've never written Rust professionally, but I did have a Haskell job for a couple of years. IME the sophistication of the type system is a double-edged sword. Sometimes it lets you concisely express important invariants; sometimes you end up spending way too much time writing elaborate types for code that isn't really doing anything very interesting. I understand the appeal of fancy type systems. In practice, I get a productivity boost from any kind of basic static typing, and then rapidly diminishing returns from the fancier stuff.
Re: Go 1.27 Interactive Tour
#209But he actually wrote those and they made sense.
Re: Go 1.27 Interactive Tour
#210>func (b Box[T]) Map[U any](f func(T) U) Box[U] {} That's completely unreadable.
`func (b Box[In]) Map[Out any](f func(In) Out)Box[Out]`
Or
`func Apply[In, Out any](in []In, f func(In) Out) []Out`
Which can be used anywhere and is not tied to a "Box"
Honestly if it wasn't written in go it'd fit right in with Java, with a Verb in the kingdom of Nouns.