News for hackers. Stuff that matters.
Re:Toward Go 2 by wheaties on Thursday July 13, 2017 @01:45PM (#14763423)
If they want to "learn" about generics perhaps they can read the literature of the past 30yrs and look at how other languages have adopted those learnings: Java, C#, Haskell, OCaml and Coq.

Look, even allowing type aliases to become part of an interface declaration would be a HUGE win. You can't currently write portable arbitrary data structures without reimplementing them with a code generator. Ugh!

Re:Toward Go 2 by seanwilson on Thursday July 13, 2017 @02:26PM (#14763850)
> If they want to "learn" about generics perhaps they can read the literature of the past 30yrs and look at how other languages have adopted those learnings: Java, C#, Haskell, OCaml and Coq.

Yeah, I find it strange how languages are trending at a glacially slow pace to having the same features that strongly typed functional programming languages have had for literally decades. It's like we're going to be using actual functional programming languages eventually but the only way it'll happen is to very slowly change what everyone is currently using in small steps.

Static types, immutability, type inference and non-null variables are popular design choices right now but they've been in functional programming languages for nearly 50 years. I'm still waiting for inductive types and pattern matching to turn up in a mainstream language and seeing them talked about like they're new concepts.

Re:Toward Go 2 by JBReefer on Thursday July 13, 2017 @02:46PM (#14763996)
C# is adding pattern matching in the upcoming release, and to your point, people are acting like it's the new hotness.
Re:Toward Go 2 by masklinn on Thursday July 13, 2017 @02:55PM (#14764080)
That's not exactly surprising, the C# community has been doing that since the beginning of the language, anything not in the language is pointless academic wankery, and as soon as Microsoft announces it it's the best innovation in computing history since Microsoft was created.

Source: got to interact with the community between the C# 1.0 and 4.0 releases (2.0 added generics, 3.0 added lambdas, neither feature was considered of any use to a Productive Developer up to the day when Microsoft officially announced them).

Re:Toward Go 2 by seanmcdirmid on Thursday July 13, 2017 @03:08PM (#14764210)
> That's not exactly surprising, the C# community has been doing that since the beginning of the language, anything not in the language is pointless academic wankery, and as soon as Microsoft announces it it's the best innovation in computing history since Microsoft was created.

That isn't true inside Microsoft. Many of the people who work on C# are the same academic wanks that work on Scala or F#. C# has a different user base from those languages though, so they still have to be careful what they add to the language, and many language features are planned 3 or 4 versions in advance.

Re:Toward Go 2 by masklinn on Thursday July 13, 2017 @04:36PM (#14764948)
> That isn't true inside Microsoft.

No, that was not intended as included in "the C# community". Hell, SPJ used to work at Microsoft (he may still do, but he used to).

> the people who work on C# are the same academic wanks that work on Scala or F#

I'm sure you mean wonks, but I liked the typo.

> C# has a different user base from those languages though, so they still have to be careful what they add to the language, and many language features are planned 3 or 4 versions in advance.

I have no issue with the evolution of C# rate or otherwise, only with a number of its users.

Re:Toward Go 2 by seanmcdirmid on Thursday July 13, 2017 @08:17PM (#14766462)
Ugh, that was bad embarrassing typo.

C# has a specific audience, and the language designers cater to them pretty well. I really really like C# as a language, and I don't mind delayed access to certain features that I already like from other languages.

You probably have a beef with some C# users not because of their choice of language, but with the field they work in (primarily enterprise) tends to breed a certain kind of attitude that other techies don't like very much.

Re:Toward Go 2 by hinkley on Thursday July 13, 2017 @05:52PM (#14765607)
Clearly they finally learned the lessons of Apple. Old is busted, new is perfect.

(says a man planning to purchase his 5th Macbook Pro later this year)

Re:Toward Go 2 by seanmcdirmid on Thursday July 13, 2017 @03:04PM (#14764178)
It is new to C#, which is slowly catching up to Scala and F# in that regards. Mads Torgesen is good friends with Martin Odersky, in fact, when I first met Mads back in 2006 or so, they were talking about adding pattern matching to C#. C# is a much more conservative language, and it makes sense it would take a while to add.

There are good reasons to use C#, so when it gets a new feature that other languages have had for years, well, it is newsworthy.

Now when will javascript get pattern matching?

Re:Toward Go 2 by herrschnapps on Thursday July 13, 2017 @04:36PM (#14764951)
It's just become a Stage 0 proposal: https://github.com/tc39/proposal-pattern-matching
Re:Toward Go 2 by seanmcdirmid on Thursday July 13, 2017 @08:18PM (#14766467)
Woo hoo, can't wait. Especially with what TypeScript can do with this.
Re:Toward Go 2 by _old_dude_ on Thursday July 13, 2017 @06:02PM (#14765689)
There is a paper, Pizza into Java: Translating theory into practice, from Odersky and Wadler at POPL 97 about how to mix generics, lambda and pattern matching in a Java like language.

C# and Java are slowly catching up :)

Re:Toward Go 2 by dvlsg on Thursday July 13, 2017 @04:21PM (#14764826)
There's a proposal for it! Stage 0? Stage 1? Don't remember what stage it's in off the top of my head, but it looks promising.
Re:Toward Go 2 by mathw on Friday July 14, 2017 @02:43AM (#14768014)
I think the reason for that enthusiasm is not so much that it's the new hotness (although it is some people's first encounter with the idea), but that it's now available in a mainstream language that their employer will actually let them use (in about five years when they finally bother upgrading Visual Studio).
Re:Toward Go 2 by masklinn on Thursday July 13, 2017 @02:50PM (#14764027)
Surely if Go is considered mainstream so is Swift?
Re:Toward Go 2 by chongli on Friday July 14, 2017 @03:44AM (#14768221)
strange how languages are trending at a glacially slow pace

Human behaviour is strange when you expect rationality. Sour grapes is such a pervasive cognitive bias that one has to wonder why it exists since it's obviously irrational. I think it's likely that it presents a major advantage in the psychology of group cohesion.

Re:Toward Go 2 by masklinn on Thursday July 13, 2017 @01:54PM (#14763512)
> perhaps they can read the literature of the past 30yrs

40, nearing on 45:

> Milner, R., Morris, L., Newey, M. "A Logic for Computable Functions with reflexive and polymorphic types", Proc. Conference on Proving and Improving Programs, Arc-et-Senans (1975)

Re:Toward Go 2 by endymi0n on Thursday July 13, 2017 @02:04PM (#14763620)
Guess what: they did.

https://news.ycombinator.com/item?id=8756683

They just weren't ready to make any of the significant tradeoffs other languages have to do in order to support them.

Whether that's a good outcome, I'm not fully sure - but there's definitely a lot of research in that area.

Re:Toward Go 2 by pcwalton on Thursday July 13, 2017 @02:21PM (#14763797)
The basic tradeoff is monomorphization (code duplication) vs. boxing (in Go speak, interface{}). The problem with saying "well, this is a tradeoff and both sides have downsides, so we won't do anything" is that the tradeoff still exists—it's just something that manually has to be done by the programmer instead of something that the compiler can do. In Go, the monomorphization approach is done with code duplication (either manually or with go generate), while the boxing approach is done with interface{}. Adding generics to the language just automates one or both of the approaches.

The idea that the monomorphization/boxing tradeoff can be dodged by not having generics in the language is a fallacy. In Go, today, you as a programmer already have to decide which side of the tradeoff you want every time you write something that could be generic. It's just that the compiler doesn't help you at all.

Re:Toward Go 2 by masklinn on Thursday July 13, 2017 @02:36PM (#14763919)
> The basic tradeoff is monomorphization (code duplication) vs. boxing (in Go speak, interface{}).

And even that is not the whole story, just because you reify generics does not mean you have to monomorphise everything, Microsoft's C# compiler only monomorphises value types.

Also Go may box in other contexts than interfaces depending on escape analysis (according to https://golang.org/doc/faq#stack_or_heap).

Re:Toward Go 2 by slrz on Friday July 14, 2017 @10:35AM (#14770078)
The FAQ entry does not state that the Go compiler boxes escaping variables (it doesn't, AFAICT). How did you arrive at that conclusion?
Re:Toward Go 2 by vram22 on Thursday July 13, 2017 @03:45PM (#14764524)
>monomorphization

Does that mean writing a separate version of an algorithm for each type or types that you want it to handle? Like a sort for ints, a sort for floats, etc., even if the logic is otherwise the same (or almost)?

Not a PL design or theory expert, just interested.

Re:Toward Go 2 by pcwalton on Thursday July 13, 2017 @03:46PM (#14764535)
Yes, it means specializing the code and generating a separate version of the function for each type it is instantiated with.
Re:Toward Go 2 by vram22 on Thursday July 13, 2017 @04:47PM (#14765065)
Got it, thanks.
Re:Toward Go 2 by willtim on Thursday July 13, 2017 @03:30PM (#14764405)
In Haskell (and I believe Scala), one can use pragma hints to specify to the compiler when to specialise, if performance becomes a problem. So I don't see this as an argument against parametric polymorphism.
Re:Toward Go 2 by slavapestov on Thursday July 13, 2017 @04:37PM (#14764963)
> The basic tradeoff is monomorphization (code duplication) vs. boxing (in Go speak, interface{}).

There is also the approach taken by Swift, where values of generic type are unboxed in memory, and reified type metadata is passed out of band. There's no mandatory monomorphization.

Re:Toward Go 2 by pcwalton on Thursday July 13, 2017 @04:54PM (#14765135)
Really by "boxing" I mean "dictionary passing" (I've also seen the term "intensional type analysis" for it). That encompasses approaches like Java as well as those of Swift.

Hybrid approaches in which the compiler (or JIT!) decides whether to monomorphize or use vtables are also possible.

Re:Toward Go 2 by slavapestov on Thursday July 13, 2017 @05:02PM (#14765202)
"Dictionary passing" is a good term for this implementation strategy, I haven't heard it named before. Do you know of any other languages that take a similar approach?
Re:Toward Go 2 by dbaupp on Thursday July 13, 2017 @05:48PM (#14765579)
Re:Toward Go 2 by slavapestov on Friday July 14, 2017 @04:34PM (#14772951)
I thought Ocaml still boxed values, so there's no equivalent of value witness tables?
Re:Toward Go 2 by dbaupp on Sunday July 16, 2017 @09:48PM (#14785127)
Sure, but it still does basically the same thing of passing around witness tables. Values happen to have a uniform representation, but this seems seems like variation within a cluster of languages that do similar thing rather than something fundamentally new.
Re:Toward Go 2 by dbaupp on Thursday July 13, 2017 @04:53PM (#14765128)
The Swift approach shares many of the same characteristics and trade-offs as "true" (aka Java-style) boxing. Of course, it does avoid some downsides of that, but also brings along some additional ones.
Re:Toward Go 2 by slavapestov on Thursday July 13, 2017 @05:07PM (#14765252)
I think the main difference is that the Swift approach ends up being more memory-efficient, eg consider Swift's Array<Int> is stored as an array of integer values compared to a Java's ArrayList<Integer> where each element is boxed.

Also the Swift optimizer can clone and specialize generic functions within a module, eliminating the overhead of indirection through reified metadata.

Re:Toward Go 2 by dbaupp on Thursday July 13, 2017 @05:53PM (#14765620)
Yes, there's some pitfalls of Java that Swift avoids, like compulsory allocation, but it fundamentally has the same style of indirection (often pointers to stack memory rather than heap, but still pointers), type erasure and pervasive virtual calls, and brings with it even more virtual calls due to needing to go through the value witness table to touch any value (which might be optimised out... or might not, especally with widespread resilience).

The compiler specializing/monomorphizing as an optimisation shows that Swift has a hybrid approach that tries to balance the trade-offs of both (like many other languages, like Haskell, and, with explicit programmer direction, Rust), but the two schemes still fall into the broad categories of monomorphizing vs dictionary passing, not something fundamentally different.

Re:Toward Go 2 by slavapestov on Friday July 14, 2017 @04:33PM (#14772944)
Oh, hi Huon.
Re:Toward Go 2 by masklinn on Thursday July 13, 2017 @02:18PM (#14763772)
> Guess what: they did.

Yeah as pointed out in that very thread by pjmlp:

    As usual, it lacks discussion about generics in:
    Eiffel
    Ada
    Modula-3
    D
    MLton
    .NET
    Rust
    Haskell
    OCaml
    Sather
    BETA
    CLU
Oh wait, they're pointing out that none of these is even remotely being discussed in the article hinting that they did not, in fact, and much prefer taking down strawmen they built from some sort of daguerreotypes of Java and C++.

> They just weren't ready to make any of the significant tradeoffs other languages have to do in order to support them.

Ah yes, the ever so convenient but never actually clarified "tradeoffs", which only ever happen for generics but oddly enough apparently don't happen for any other feature.

> there's definitely a lot of research in that area.

That there is, would be nice if Go's designers ever bothered actually using it.

Re:Toward Go 2 by fmap on Friday July 14, 2017 @07:37AM (#14769004)
Nitpick, but the idea is even older, going back at least to Girard (1971) and Reynolds (1974). :)

I don't know exactly what the problem is for Go. There are tradeoffs, e.g., just with the type system: impredicative, stratified or predicative quantification, implicit subtyping or explicit type instantiation, value restrictions, variance handling for mutable inductive types, Hindley-Milner or bidirectional typechecking, etc. There are more tradeoffs with the implementation. Fortunately, these are all well understood by now.

However, it is also true that many mainstream languages famously got generics wrong. What's most infurating about this situation is that a lot of research just gets ignored. If the question is really "should Go have generics whose design is based on C++ templates and/or Java generics" then the only sane answer is a resounding no.

Re:Toward Go 2 by lngnmn on Friday July 14, 2017 @02:53AM (#14768049)
The classic Wizards lecture on generic '+ is good enough argument.

The numeric tower of lisps is the canonical use case of what generic functions are good for.

Smalltalk, the second greatest language after Lisp, is also good example.

Re:Toward Go 2 by pjmlp on Thursday July 13, 2017 @02:21PM (#14763796)
You forgot CLU in 1975. :)