News for hackers. Stuff that matters.
Re:Go 2, here we come by glangdale on Thursday November 29, 2018 @07:40PM (#18565605)
I'm pretty excited by the idea of Go getting generics. This has always been my deal-breaker issue with Go and I'm glad that what appeared to be a disingenuous "let's pretend to be hunting for the truth until people go away" stance was actually really a hunt for the truth! Goes to show that you shouldn't make snarky snap judgments.

As for all the folks claiming they'll leave Go if it gets generics, it's faintly reminiscent of Mac fanboys claiming PowerPC chips were the Very Best right up until this was obviously not true. C++ generics are a PITA in many ways, but you can be insanely productive in the STL without having to hand-roll everything and with good type safety.

Despite the pain, I've been amazed at how easily you can build up some really complex data structures as pretty much one-liners ("Oh, I need a vector of maps from a pair of Foo to a set of Bar") that would either take a preposterous amount of code (or be a type-unsafe disaster waiting to happen) without generics.

Hopefully the final Go 2 generics proposal will capture some of this goodness without some of the horrifying C++ issues (error messages, bloat, sheer brain-numbing complexity).

Re:Go 2, here we come by endorphone on Thursday November 29, 2018 @08:46PM (#18565897)
"As for all the folks claiming they'll leave Go if it gets generics"

Baffled at this claim I searched and found one person who really seemed to be saying "if Go changes dramatically...".

This recurring notion that Go fans are anti-generic is not rooted in reality. Instead they simply didn't buy the "either it's there or the language is useless -- generics or bust!" argument that pops up in every Go discussion. It's a fine, if imperfect, language without generics. It's a better language with them.

Re:Go 2, here we come by comex on Thursday November 29, 2018 @09:59PM (#18566262)
I don’t know about threatening to leave Go, but I’ve definitely seen a lot of posts claiming Go is better without generics.
Re:Go 2, here we come by stcredzero on Thursday November 29, 2018 @10:34PM (#18566428)
The more nuanced and better side of that is that any language is better without badly designed and badly implemented generics.
Re:Go 2, here we come by dnautics on Friday November 30, 2018 @04:00AM (#18567735)
Maybe the even more nuanced side is that a language that can't conjure a safe and well designed generic has a badly designed type system in the first place
Re:Go 2, here we come by stcredzero on Friday December 07, 2018 @12:07PM (#18629352)
That sounds axiomatic.

How would you define "badly designed?" I can imagine a context where the "well designed" type system is very plain.

Re:Go 2, here we come by ryeguy on Thursday November 29, 2018 @08:18PM (#18565791)
I really don't get the people opposed to generics. Is there actually a cross between experienced developers who have come from languages that have generics and understand them, yet don't want them in go? If so, why, and what do they use instead? Because go has no compromise-free answer for generics. You either lose type safety, maintainability, or performance.

I suspect a lot of the generics hate is due to a large chunk of the community coming from dynamically typed languages. In which case they're just have a negative reaction to unfamiliarity.

Re:Go 2, here we come by erikpukinskis on Thursday November 29, 2018 @08:43PM (#18565888)
Increasing language surface area will generally increase the complexity of all api surfaces written in the language. This has obvious costs.

It’s true genetics will shrink some specific APIs, where they are a good fit.

But they will also be used opportunistically by developers excited to push their boundaries.

Of course you can say “just don’t do that” which works if you have a tightly controlled codebase. But most code is not that, and will be handed off to novices over and over for fixes.

So, it’s a question of whether you cater to the advanced developer who can capably handle a vast toolset, or do you commit as a community to more rudimentary tools, in order to reap the rewards of systemic simplicity.

There is no right answer.

I believe in the future all languages will fork into a simpler novice subset for general use and an expansive language for infrastructure. These will both be valid in the same parser, but the subset will be quarantined at the package management level.

Go, EZ-Go, C, EZ-C, etc

I am writing all of my code in EZ-JS

Re:Go 2, here we come by closeparen on Thursday November 29, 2018 @09:39PM (#18566164)
It’s C’s lack of complexity which makes it necessary to do dangerous things for everyday purposes. I’d much rather a novice interact with generics, where the compiler is a safety net, than with void * and interface{}.
Re:Go 2, here we come by dullgiulio on Friday November 30, 2018 @06:34AM (#18568347)
void * and interface{} are not going away when generics come. Try searching for "Object" or "<?>" in some Java code...
Re:Go 2, here we come by Filligree on Friday November 30, 2018 @11:46AM (#18570344)
Which is why Go 1's type system was a bad decision. interface{} should have never existed in the first place.

At least it'll definitely get rarer.

Re:Go 2, here we come by themihai on Thursday November 29, 2018 @10:13PM (#18566336)
>> It’s C’s lack of complexity which makes it necessary to do dangerous things for everyday purposes.

That's not the case anymore. We have C++ which has generics and "fixed" C's lack of complexity for sure...Now for some 'weird' reasons some people still use C. Wonder why ?

Re:Go 2, here we come by wbl on Friday November 30, 2018 @12:15AM (#18566889)
Compare C++ to Standard ML. Simplicity is compatible with generics.
Re:Go 2, here we come by pjmlp on Friday November 30, 2018 @03:32AM (#18567613)
It is not the case since 1993, when CFront was dropped.

In certain domains like UNIX like OSes, I don't see C ever going away, due to the infrastructure, symbiotic relation with the OS that gave it birth, and the culture.

Re:Go 2, here we come by m90 on Friday November 30, 2018 @11:52AM (#18570393)
> Of course you can say “just don’t do that”

This exactly. The beauty of Go is that currently you do not need a book called "Go, The Good Parts".

Yes, generics would make a lot of things easier, but almost everything more complex and footgunnish.

Re:Go 2, here we come by jpfed on Thursday November 29, 2018 @11:05PM (#18566575)
>I believe in the future all languages will fork into a simpler novice subset for general use and an expansive language for infrastructure. These will both be valid in the same parser, but the subset will be quarantined at the package management level.

I don't think we'll see this happen much for existing languages, but it could be a very interesting angle for a newly-designed language (or rather pair of languages).

Re:Go 2, here we come by Tarq0n on Friday November 30, 2018 @04:56AM (#18567977)
To some extent this is already happening in languages popular for machine learning. Libraries are written in C/C++ and the users just glue things together with very accessible API's.
Re:Go 2, here we come by erikpukinskis on Friday November 30, 2018 @02:49PM (#18572045)
It’s already happening. For example many people code in a subset of C++.
Re:Go 2, here we come by cdoxsey on Thursday November 29, 2018 @10:40PM (#18566470)
Although ultimately I do want generics in Go I am afraid they will make the language more difficult to use and understand. Generics in c++, c#, scala, java, etc all tend toward being very complex and change the way programs are written.

The focus moves towards a taxonomy of types and developers (myself included) sometimes get stuck on difficult type problems. There's something about trying to preserve type safety which sets the bar extremely high for bypassing the type system when there's not an easy solution and before you know it you've wasted 2 or 3 days writing code which doesn't actually do anything but placate the compiler.

And often that type-safe concoction you create is almost indecipherable when you come back to it later.

For example here's a project I worked on recently which cached a concrete version of a generated method using generics:

https://github.com/DataDog/dd-trace-dotnet/blob/develop/src/...

Used like this:

    var originalMethod = DynamicMethodBuilder<Func<object, byte[][], object, object, bool, T>>
               .GetOrCreateMethodCallDelegate(
                    redisNativeClient.GetType(),
                    "SendReceive",
                    methodGenericArguments: new[] { typeof(T) });

At least for me that was really hard to figure out how to do and I still have to squint to see what the heck its doing. The non-generic version wasn't type-safe and it wasn't as fast, but it sure was a lot easier to read and understand.

And to give some sense of the complexity involved, Rob Pike mentioned in his talk that the proposal spec for adding generics to Go is longer than the spec for the entire language.

I think the complexity is worth it, but I just hope we can be cautious about how and where generics get used in real-world code, otherwise we'll end up with gobbledy-gook that only experts can decipher... and that would be really sad, because the promise of Go was a language normal engineers could be productive with.

Re:Go 2, here we come by delhanty on Thursday November 29, 2018 @11:39PM (#18566724)
>Generics in c++, c#, scala, java

They're not all the same though are they.

C++/CLI had both compile-time generics (templates) from C++, and run-time generics from the CLR. And they could be complementary at times.

For compile-time generics, Dlang are a lot more sane that C++, having had the benefit of coming later, and dumping C compatability.

Similarly, CLR (C#, ...) generics had the benefit of being designed having seen Java first, so IIRC they're baked into the CLR. CKR generics were derived from work done at MS Research Cambridge, and I seem to remember Don Syme (F# creator) being rather proud of them. Disclosure: I was contracting at MSR Cambridge back in 2007.

Anyway, the golang designers will be aware of these implementations, so hopefully they'll come up with a nice design.

Re:Go 2, here we come by kminehart on Friday November 30, 2018 @12:05PM (#18570484)
I'll answer from my perspective.

I don't hate generics. I see the value of generics, especially after having worked with Go for so long; I've had to do some mental gymnastics to get around not having proper generic collections.

That being said, I'm not excited about seeing generics in other people's code. The added complexity doesn't really solve problems I have anymore.

That being said, I'm actually way less excited about overloading.

I think generics and function overloading is going to make me think "where the hell is this coming from?" a lot more often and then I'm going to need to load it up in my IDE, or vim with way too many plugins, and start following definitions.

Re:Go 2, here we come by ramenmeal on Thursday November 29, 2018 @08:29PM (#18565842)
I think the main fear people have isn't the concept of generics, but the implementation of generics. Go's primary goal is simplicity, and implementing generics isn't simple.
Re:Go 2, here we come by ryeguy on Thursday November 29, 2018 @10:09PM (#18566318)
But like I said, Go doesn't have an answer for generics. So the complexity just lives in userland code instead of the language itself. The problem and complexity doesn't go away.
Re:Go 2, here we come by stcredzero on Thursday November 29, 2018 @10:38PM (#18566455)
If the choice is between badly implemented generics, and having the problem manifested as userland code, I'll take the latter. I've actually had to debug C++ production code produced by the confluence of templates that had no source code of it's own. At least with boilerplate, you can simply see what's going on right there.
Re:Go 2, here we come by bmon on Thursday November 29, 2018 @10:00PM (#18566268)
If language features were free we'd likely have had generics for a long time now. Unfortunately generics is a trade-off: you get development speed/ease and pay for it in compile time, binary size and/or execution speed.

This seems to be slowly changing, but Go was designed to be a solution to Google problems - python being slow but some c++ applications taking literal hours to compile. Keeping that perspective in mind makes it easier to understand why Go maintainers have not accepted an implementation of generics into the language yet.

Re:Go 2, here we come by atilaneves on Friday November 30, 2018 @07:47AM (#18568677)
If you maintain type safety, you'll pay in increased compile times with or without generics. You'll either hand-roll an implementation for each type (https://golang.org/pkg/sort/) or you'll generate code before actually compiling. The problem doesn't go away.
Re:Go 2, here we come by IshKebab on Thursday November 29, 2018 @09:24PM (#18566092)
It's people that have seen the abuses of C++ templates. They're very powerful and therefore people tend to want to use them for really complicated things.

Look up things like SFINAE and compile time metaprogramming. Templates were not intended for those things. When they work they're ok, but if they go wrong good luck following the 10 line error message.

Here's an example from Rust:

https://www.reddit.com/r/rust/comments/5nifpm/they_said_rust...

That's what people don't want.

Re:Go 2, here we come by johncolanduoni on Thursday November 29, 2018 @11:45PM (#18566748)
That's not compile time metaprogramming at all, it's just series of wrapped closures. It's basically the sequence of function call names preceding that call backwards, with different capitalization.
Re:Go 2, here we come by majewsky on Friday November 30, 2018 @05:17AM (#18568049)
The big difference between C++ and Rust is that, when you scroll down in this Reddit thread, it shows that the Rust guys have a clear path for fixing this issue, whereas there is no fix for this in C++ (that I'm aware of).
Re:Go 2, here we come by glangdale on Sunday December 02, 2018 @12:07AM (#18580920)
Ha ha, "10 line error". [ Puts on Monty Python voice] "What I wouldn't give for a 10 line error..."

Incautious use of the template mechanism - or just a minor typo - has given me couple screenfuls in the past.

Re:Go 2, here we come by int_19h on Thursday November 29, 2018 @09:46PM (#18566193)
Any sufficiently advanced type system can be used for compile-time metaprogramming - that's just an inevitable side effect of a type system expressive enough to capture all the more convoluted (but still plenty common) cases without hacks like interface{}.
Re:Go 2, here we come by libria on Thursday November 29, 2018 @10:05PM (#18566295)
I'm of 2 minds. I come from a Java background so I've personally wanted generics. ORM's are 1 use case that comes to mind.

But. There are many modern languages that already have generics. Why can't Go be that one that doesn't cave in and remains powerful in its niche and perhaps may never be the preferred tool in other areas? Why must it be useful for web development, microservices, DAL, etc.?

Any implementation of generics in Go will come with complexity tradeoffs.

I feel like the community learns more about languages and software engineering when we maintain language diversity and see the pros and cons of each approach in practice.

I want generics for selfish reasons, but would also like to see how a modern strongly-typed language solves problems without it.

Re:Go 2, here we come by idle_zealot on Thursday November 29, 2018 @10:37PM (#18566445)
As someone who quite likes generics, I'd love to see how a modern strongly-typed language solves problems without them. And I think that's what the Go community and developers have been trying to do up until now. It looks like they're giving up. I'm not sure whether we'll ever find another way to tackle composition/scalability as effectively as generics do, but such a technique would be fascinating to see.
Re:Go 2, here we come by Anaminus on Thursday November 29, 2018 @10:01PM (#18566280)
For me at least, the reluctance comes from the new proposal process not having yet proved itself for large backwards-incompatible features. I want to be assured that a Go with generics is a Go with a really well-integrated feature, and not some grafted mutant appendage whose only real purpose is to appease the greater community.

It's great that they're trying out the process on smaller, more simple proposals. My hope is that this system will either produce really good features, or reveal that there are simply no satisfying solutions.

Re:Go 2, here we come by icholy on Saturday December 01, 2018 @01:57PM (#18578227)
Coming up with a complex solution is much easier than finding a simple one. Go forces you to find those simple solutions. There are places where generics are the only solution, but they also enable lazy design.
Re:Go 2, here we come by burtonator on Thursday November 29, 2018 @07:52PM (#18565663)
I'm hoping things like generics can just be accepted to be a good idea moving forward and that we can all agree that languages without them are handicapped.

I still fell things are up in the air about exceptions but maybe we can just agree on generics which would make me feel better.

Using go without generics just felt insane to me...

Re:Go 2, here we come by cultus on Thursday November 29, 2018 @08:15PM (#18565774)
Yeah, Go has some really fantastic aspects around tooling and a good concurrency story, but it's otherwise such a huge step backwards. I can't fathom the reason for not having generics. It's such a simple, completely common-sense abstraction.

Things like typeclasses or multimethods offer vastly more abstraction power. These are (a bit) more difficult to understand, but you certainly don't have to be a genius (take it from me). I can kind of get why a language targeted towards "average" programmers might want to omit these.

But here's the thing: The less abstraction power a language has, the more complexity must be handled by the developer. This leads to things like Java Spring, which you do have to be a genius to understand.

Re:Go 2, here we come by tatersolid on Thursday November 29, 2018 @09:41PM (#18566173)
s/genius/masochist/g
Re:Go 2, here we come by gameswithgo on Friday November 30, 2018 @07:19AM (#18568545)
>"Oh, I need a vector of maps from a pair of Foo to a set of Bar"

This is a fun example since you can do that in Go now, as it comes with a generic vector (essentially) and map :D

I think that was the design decision, 95% of generics use cases are covered by having growable arrays and maps, so why clutter the language with generics.

I like generics, I think most people who dislike them are coming from C++ templates, which has downsides that don't exist in newer generics systems (such as in C# or F#)

Re:Go 2, here we come by mrfusion on Thursday November 29, 2018 @09:22PM (#18566077)
What are generics?0
Re:Go 2, here we come by w8rbt on Thursday November 29, 2018 @10:38PM (#18566456)
Rather than writing multiple functions that take different types as args and return different types (say int8, int16, int32, etc.) but do the exact same thing, with generics, you can instead write one function that takes a number (which could be any int type) and return a number, and you only wrote one function. That is generics in a nutshell. The function is generic, not specific to one type.

Generics allow for less code, thus they are easier to debug, test and reason about. I've used them a lot in C++ and I do miss them in Go. It's not a deal breaker for me either way, but for people who write larger, more complex code, not having them makes it more difficult (more code to write, test and maintain).

C does not have generics either. Go is a lot like C in this regard.

Re:Go 2, here we come by pjmlp on Friday November 30, 2018 @12:36AM (#18566986)
C has minimal generics support since C11.
Re:Go 2, here we come by mrfusion on Thursday November 29, 2018 @10:44PM (#18566490)
Wouldn’t this make golang slow though? Isn’t that what people claim makes python slow?
Re:Go 2, here we come by shakna on Thursday November 29, 2018 @11:07PM (#18566583)
In a ahead-of-time compiled language probably not. The generic function is just a code generator for several functions that will get called in the right places, generated and put in the right places by the compiler, and then get optimised as per normal.

And no, being a dynamic language is not what makes Python slow. Lua, Nim and Scheme are all examples of dynamic languages with fast implementations.

Re:Go 2, here we come by lasagnaphil on Friday November 30, 2018 @01:31AM (#18567185)
- Generics do not make runtime performance slow, but it sure does make compile times slower (although it really depends on the type system and its implememtation) For example, C++ templates, although very powerful, makes build times order of magnitudes slow when used poorly. One of the most important features of Go is its fast compile times, but generics/contracts can potentially slow it down a lot.

- Yes, being a dynamic language slows it down a lot. For example, when evaluating a+b, the interpreter has to check the types of the variables a and b at runtime beform performing the right form of addition (it might be an int, or a string, you dont know). Even with JIT (just in time compilation) the compiler has to initially “guess” that the variables are numbers, and fall back if that is not the case. Statically typed languages do not have this problem, because you know the types of variables beforehand at compile time.

By the way, Nim is not a dynamic language. And LuaJIT is one of the the fastest dynamic language implementations because the language is very simplistic (compared to Javascript/Python/Ruby) and Mike Pall is a robot from the future...

Re:Go 2, here we come by shakna on Friday November 30, 2018 @01:49AM (#18567261)
> the interpreter has to check the types of the variables a and b at runtime beform performing the right form of addition (it might be an int, or a string, you dont know)

That's not necessarily true. In Scheme, a very dynamic language, the compiler will generally make choices about memory layout of the various values before launching into the evaluation phase. Where safe or possible to do so, it will probably reduce the number of choices that will have to be made at runtime, at compile time. The interpreter may not have to lookup what the data type is, because it may just have two bits of memory and an instruction to call. You can know what the data will be, and optimise for it. [0]

There's no reason that: b = 1; c = 2; a = a + b needs to be slower than a = 1 + 2 if b and c are never referenced before or after. But in Python, it is, because the design makes it harder to know whether or not an object should get optimised away.

> And LuaJIT is one of the the fastest dynamic language implementations because the language is very simplistic

Right, Lua is dynamic, and one of the design choices was making it simple, and so it's easier to make it faster.

[0] http://home.pipeline.com/~hbaker1/CheneyMTA.html

Re:Go 2, here we come by lasagnaphil on Friday November 30, 2018 @02:02AM (#18567311)
Didn't know about Scheme. Thanks for the link!
Re:Go 2, here we come by shakna on Saturday December 01, 2018 @03:08AM (#18575842)
If you've never met Scheme, then SICP [0, 1, 2] may be something that can change the way you program. It certainly made me better, or at least a deeper understanding.

[0] https://www.youtube.com/watch?v=2Op3QLzMgSY&list=PLE18841CAB...

[1] https://www.amazon.com/Structure-Interpretation-Computer-Pro...

[2] https://web.mit.edu/alexmv/6.037/sicp.pdf

Re:Go 2, here we come by mrfusion on Thursday November 29, 2018 @11:19PM (#18566634)
Now I have to ask, what would you say does make python slow?
Re:Go 2, here we come by shakna on Thursday November 29, 2018 @11:46PM (#18566752)
Design choices, to be blunt.

Some of it is simple stuff, like CPython being interpreted, so PyPy get's a huge boost by JITting.

Some of it is much harder stuff, like the way Python is designed to store objects in memory (a list is a pointer to a contiguous space of pointers to things that might be pointers...), and everything that hangs off each object (everything has a dict), and the awful GIL ([0]). Awful for performance, great for thread-safety.

Every single part of a language design has trade-offs. It depends on what you're trying to do whether or not they help you, or hinder you.

Sometimes you change how you're doing things because priorities change, and get a radical improvement, like Python's 3.6 Dict. Most of the time, you don't. One step at a time. Not being able to break backwards compatibility hinders the designer, if they realise a trade-off they've made was a mistake. So you get stuck with some features you'd rather not have.

[0] https://wiki.python.org/moin/GlobalInterpreterLock

Re:Go 2, here we come by johncolanduoni on Friday November 30, 2018 @12:19AM (#18566908)
To be fair, all of the aspects of the language itself you mentioned apply to JavaScript (save perhaps differences in how literally it takes `int`, etc. being objects vs. JavaScript's unboxed `number`, etc.). I suspect most of the difference is that one has had three of the largest corporations in the country competing to have the fastest implementation, in some cases for decades, and the other hasn't.
Re:Go 2, here we come by zbentley on Friday November 30, 2018 @08:23AM (#18568853)
I think a bigger reason is that Python prioritizes simplicity of internal implementation.

To be sure, the internal implementation of CPython (the only one I'm familiar with) is complicated as hell. But it's a whole lot simpler than any fast JavaScript runtime I've ever used. I think that's the result of a conscious choice by the maintainers/BDFL/community in Python, not a side effect of it having less funding (assuming that's the case).

Re:Go 2, here we come by gameswithgo on Friday November 30, 2018 @12:50PM (#18570862)
The primary difference is Javascript has serious JIT's as the normal case of running Javascript while Python is normally interpreted.
Re:Go 2, here we come by notduncansmith on Friday November 30, 2018 @11:42AM (#18570318)
I don’t think it necessarily impacts the runtime (though it could).

This could be my naïveté, but I don’t see why generics in Go couldn’t work analogously to the way they do in TypeScript, just a way for the compiler to verify the correctness of code accessing an interface{}.

Re:Go 2, here we come by w8rbt on Thursday November 29, 2018 @11:03PM (#18566565)
No. C++ is very fast. It has generics.
Re:Go 2, here we come by AsyncAwait on Friday November 30, 2018 @07:11AM (#18568506)
Also, very slow to compile.
Re:Go 2, here we come by sobani on Friday November 30, 2018 @05:40AM (#18568138)
A simple example of generics is generic collections.

Without generics, if you need a list filled with TPS Reports you basically have 2 choices:

- Use the 'untyped' List that deals with objects. You will have to cast to TPS whenever you look up an item and you will have to make sure that no one accidentally adds a Timesheet to this list.

- Write a custom TpsReportList. I'm sure you'll be able to write an implementation as efficient as the language creators. Oh an if you copy and past from TimesheetList, don't forget to check all names. `tpsReports.AddTimesheet()` is just embarrassing.

With generics, you will have a `List<T>` type. If you need a list of TPS Reports you will use the type `List<TpsReport>`. If you need a list of Timesheets, you will use `List<Timesheet>`.

You can't add the wrong type of item to such a list, you will always get the declared item from that list and you can't assign an instance of one type to a variable of the other type.