News for hackers. Stuff that matters.
Re:Go generics are not bad by bediger4000 on Friday July 08, 2022 @06:29PM (#32028430)
This is good to hear.

There's something like a cognitive bias that makes me leary of genetics (in any language). Once you have some cross cutting feature, generics or object orientation, or in functional programming, those functions of type 'a->'a, or assembly language address modes, people get bogged down by trying to make everything in their work generic or object oriented or "orthogonal". Lemire's example is relevant to his topic, but it could be a poster child if someone else did it. They need to sum an array of int. So they spend 4 days getting a function and a generic type that can sum every numerical type, rather than 15 minutes to sum int arrays.

What's this called?

Re:Go generics are not bad by nicoburns on Friday July 08, 2022 @06:45PM (#32028665)
> They need to sum an array of int. So they spend 4 days getting a function and a generic type that can sum every numerical type, rather than 15 minutes to sum int arrays.

More likely they install a 3rd party generic sum function and don't have to write their own at all. Well, in the case of a sum function it's probably only 15 minutes to write to generic version, and there may even be one in the standard library. But consider something like a concurrent hash map. In Rust, You just `cargo add dashmap` and you have one that works with any type that works with a regular hashmap. This isn't possible without generics.

Re:Go generics are not bad by e12e on Saturday July 09, 2022 @02:15AM (#32032363)
>> They need to sum an array of int. So they spend 4 days getting a function and a generic type that can sum every numerical type, rather than 15 minutes to sum int arrays

> More likely they install a 3rd party generic sum function and don't have to write their own at all. Well, in the case of a sum function it's probably only 15 minutes to write to generic version

I'm not sure "sum over an array of int" is a great "simple" example - how do you guarantee that the result can fit in an int?

This is (one reason) why Julia implemented a full, scheme-style, numeric tower right in the core language.

Sure there are some examples where you can sum N Mbit numbers into an Mbit number, and either want overflow, or is somehow "confident" things will fit. But in general it's just lazy "no one will ever have such a large shopping cart"-design...

Re:Go generics are not bad by anonymoushn on Saturday July 09, 2022 @03:28AM (#32032737)
> This is (one reason) why Julia implemented a full, scheme-style, numeric tower right in the core language.

What's the result of adding two i32 then? Is it an i64? Doesn't the accumulator need to get wider on some additions, since adding an i64 to an i32 might also overflow an i64?

Re:Go generics are not bad by e12e on Saturday July 09, 2022 @07:59AM (#32034203)
Actually, the default is overflow I now realize. But there is also automatic promotion, so:

    0 + typemax(UInt8)+ typemax(UInt8)
    510
While adding UInt8 first, then an implicit Int64 causes overflow:

    typemax(UInt8)+ typemax(UInt8)
    254
(ie: 255 overflows to 254 - or is promoted and sums to 510)

See: https://docs.julialang.org/en/v1/manual/integers-and-floatin...

Try: https://julialang.org/learning/tryjulia/

Re:Go generics are not bad by Beltalowda on Friday July 08, 2022 @08:44PM (#32030102)
The sum example is simple, but very often have I seen people write general solutions for business logic or whatnot that you can't just get from a 3rd party package, when it wasn't at all clear that's needed. I have done this myself too.
Re:Go generics are not bad by TheDong on Friday July 08, 2022 @06:57PM (#32028845)
> What's this called?

The blub paradox https://wiki.c2.com/?BlubParadox

Re:Go generics are not bad by mseepgood on Saturday July 09, 2022 @12:02AM (#32031511)
I don't think that's what the blub paradox is. I think the parent comment is arguing the opposite. People who believe in the blub paradox are usually so smug and full of themselves that they don't pursue simple solutions.
Re:Go generics are not bad by TheDong on Saturday July 09, 2022 @02:58AM (#32032571)
The parent commenter is dismissing generics, and various other features, as something that "bogs people down" and makes them write worse code.

This is the essence of the blub paradox, thinking that people who use a feature are simply misusing it, after all in blub you don't have generics and things are fine. They're just making things more complicated than they need to be.

The blub programmer isn't necessarily smug, they just make excuses to dismiss more powerful language features that they are not sufficiently familiar with.

Re:Go generics are not bad by fauigerzigerk on Saturday July 09, 2022 @05:20AM (#32033322)
The problem with the blub argument is that it heavily relies on the existence of an undisputed power hierarchy among programming languages. Only s/he who is an expert in the most powerful language is in a position to pass judgement on all other languages.

I don't know how to resolve disputes over the relative power of programming languages on that basis. Are dynamic languages more or less powerful than static languages with advanced type systems? I don't know. What's the definition of power?

But I think what we can take from blub is that familiarity with one language or set of features is an insufficient basis for dismissing other, unfamiliar, languages or features.

Re:Go generics are not bad by nyanpasu64 on Saturday July 09, 2022 @07:25AM (#32033983)
Having used trait-based generics, I think that trying to piece together what's going on in typenum, the GAT stabilization process, https://hirrolot.github.io/posts/rust-is-hard-or-the-misery-..., num-traits, and variance and https://github.com/rust-lang/rust/issues/25860 is absolutely bogging me down. I feel (and observe as people try modeling contracts in traits) that trying to build code which plays well with open-ended type parameters is inherently more difficult than building a correct solution for a single type. Generally I'd prefer a more focused less generic solution which corresponds to a single function in the generated machine code (modulo inlining), except in cases where generics (over a small or open set of types) provide a clear benefit (collections are a clear example, IMO point-free .iter().map().sum() style is usually a regression in readability).
Re:Go generics are not bad by bediger4000 on Saturday July 09, 2022 @12:06PM (#32036405)
I'm not dismissing generics or object orientation or orthogonal addressing modes or 'a x 'a -> 'a functions. I am observing that there's a trap that a lot of people fall into, where they spend inordinate amounts of time making everything as generic as possible at the expense of time-to-market, and understandablity.

A good class or generic is hard to beat. Unfortunately the understanding of the details that make something a good class requires experience and experiments. Maybe I am talking about premature optimization, writ larger, or in design.

Re:Go generics are not bad by ShamelessC on Saturday July 09, 2022 @03:15AM (#32032667)
Same thing happens in the other direction too e.g. Rust coders trying to use Python without familiarity with dynamic scripting languages and dismisses the whole language as a footgun (despite Rust’s learning curve being harder, for instance).
Re:Go generics are not bad by nvartolomei on Friday July 08, 2022 @06:53PM (#32028794)
This was just discussed here in another front page post “Why DRY is the most over-rated programming principle” https://news.ycombinator.com/item?id=32010699
Re:Go generics are not bad by dataangel on Saturday July 09, 2022 @01:24AM (#32032024)
Writing a generic function to sum an array of any numbers type is trivial in C++. There's no 4 day research period, even if you're new to templates.

    template<class T>
    T sum(T* x, size_t size)
    {
        T acc = 0;
        for(size_t i = 0; i < size; ++i)
            acc += x[i];
        return acc;
    }
Re:Go generics are not bad by e12e on Saturday July 09, 2022 @02:16AM (#32032370)
Isn't acc and the xs same width here? So you risk overflow?
Re:Go generics are not bad by xigoi on Saturday July 09, 2022 @05:53AM (#32033474)
Yes, obviously calculating the sum of fixed-width integers may result in overflow.
Re:Go generics are not bad by umanwizard on Friday July 08, 2022 @08:36PM (#32030032)
Re:Go generics are not bad by anyfoo on Friday July 08, 2022 @07:41PM (#32029466)
> those functions of type 'a->'a

Polymorphism. But note that there is only one function of type 'a->'a for all 'a, and that's the identity function.

Re:Go generics are not bad by rachitnigam on Friday July 08, 2022 @07:56PM (#32029640)
Not quite. This also has the type ‘a -> ‘a:

def nitpick(x): throw “foo”

Re:Go generics are not bad by VirusNewbie on Friday July 08, 2022 @11:10PM (#32031170)
that's not a function (in the pedantic sense).

technically a partial function, or you could say that every function is really of the type A -> Exception \/ A

Re:Go generics are not bad by Cobord on Saturday July 09, 2022 @12:16AM (#32031597)
But you should not be writing functions that make you think about the fact that Hask is not really a category.
Re:Go generics are not bad by anyfoo on Friday July 08, 2022 @08:37PM (#32030034)
You're right, and foo x = foo x also qualifies as 'a -> 'a. The only function that always terminates is the identity function, then.
Re:Go generics are not bad by xigoi on Saturday July 09, 2022 @05:48AM (#32033453)
A function in the mathematical sense is total (always terminates).
Re:Go generics are not bad by tsimionescu on Saturday July 09, 2022 @01:35AM (#32032097)
We could throw some unsafePerformIO in there and do anything else as well, but if course we would have a function in the mathematical sense anymore.
Re:Go generics are not bad by xigoi on Saturday July 09, 2022 @05:51AM (#32033462)

    func sum[Number](nums: openArray[Number]): Number =
      for num in nums:
        result += num
Took about a minute.
Re:Go generics are not bad by alanbernstein on Friday July 08, 2022 @06:44PM (#32028642)
Premature abstraction?
Re:Go generics are not bad by jollybean on Friday July 08, 2022 @07:48PM (#32029548)
'Premature Abstractulation'

I'm sorry. I had to do it. Ban me.

Re:Go generics are not bad by tomcam on Friday July 08, 2022 @09:38PM (#32030444)
I'll allow it