Live data from Hacker News

Show HN: Fo: An experimental language which adds generics on top of Go

github.com

91–100 of 125 posts

Re: Show HN: Fo: An experimental language which adds generics on top of Go

#91

Earlier quoted context omitted.

One of my first Go programs was based on this image resizer: https://github.com/nfnt/resize/blob/master/resize.go#L109 Do you have any recommendations for how to write this code without generics, without sacrificing performance, and without hundreds of lines of duplicated code?

After only a quick glance at your code, I'd try to tackle it by wrapping my own interface around the different image types (which you could argue the image standard lib should've already done for you). The interface will define CreateWeights and Resize operations. Each implementing type will consist of a struct with an embedded field of each image type you're handling, and passthrough calls to the underlying function…

Image allows you to do this more or less. The problem to be solved here is that if you write the resize logic just once, you are accessing pixels from the image via the interface in a tight loop, and most of the time taken by your image resizer is the overhead of that access.

Re: Show HN: Fo: An experimental language which adds generics on top of Go

#92

Earlier quoted context omitted.

After only a quick glance at your code, I'd try to tackle it by wrapping my own interface around the different image types (which you could argue the image standard lib should've already done for you). The interface will define CreateWeights and Resize operations. Each implementing type will consist of a struct with an embedded field of each image type you're handling, and passthrough calls to the underlying function…

Image allows you to do this more or less. The problem to be solved here is that if you write the resize logic just once, you are accessing pixels from the image via the interface in a tight loop, and most of the time taken by your image resizer is the overhead of that access.

Interesting, you have a point actually. I would have thought that the performance cost of calling through an interface would be negligible, but I'm wrong about that:

https://github.com/golang/go/issues/20116

The issues linked at the end there are interesting reads, and maybe they'll do something about this by Go 1.11.

Having said all that, will an implementation of your code with generics be all that much faster? Or slower? Of course, those are not answerable questions in practice with today's tools :-)

Re: Show HN: Fo: An experimental language which adds generics on top of Go

#93
post #31

Earlier quoted context omitted.

Isn't interface{} a generic?

Noooooo! interface{} is a raw type. It’s like “Object” in java land. It has no meaning in its own right, and needs to be carefully checked whenever you want to actually use it. The proliferation of interface{} across the go ecosystem is really unfortunate, and will be hard to correct once generics are finally supported. I haven’t dug too far into the Fo code yet, but I’d guess that it’s doing something akin to type e…

Well, it is in the literal sense that it enables generic algorithms, just like the one, unnamed static type in all dynamic languages. But of course this is pedantry and no one really means this when talking about generics.

Re: Show HN: Fo: An experimental language which adds generics on top of Go

#94
post #79

Nice work. But, serious question - When going with parameterized types I know from Haskell how you always end up needing one more language extension and always end up banging your head against the wall that separates types and values a little more. At least if you're not a math genius, but probably even then. And from Java I know that there's a pretty trivial example that showcases how its Generics implementation is…

Haskell is a proving ground, so it's picked up a number of ideas that never quite panned out. I think you can identify a core set of features that are pretty reasonable. I think that'd be type classes, constraints, and functional dependencies. If they made a handful of extensions standard (GADTs, etc.) it would mostly Do What You Want without a lot of prodding. > And from Java I know that there's a pretty trivial exa…

> There's a well known limitation that mutable containers (and they're all mutable in Java) need to be invariant, but that doesn't make them unsound.

I see. Yeah I don't know precisely what these terms mean. If the creators of Generics mistakenly made them covariant, that only goes to show that maybe it's a little too complicated. IMHO.

To be more precise, what we want to do might be too complicated for practical (i.e. relatively simple) type systems to describe. So, why bother at all? Better learn how to structure programs simple enough to make them obviously correct (i.e. mistakes will be obvious and can be easily fixed). Instead of catering to the needs of impractical type systems. I think that's why C is still so popular: It removes most of the boilerplate (i.e. strides for array indexing, arithmetic operators, structs, other ABI things) but gets out of the users way if s/he needs to disregard these constraints for a while.

Even in C, there is a similar problem with const compatibility of pointers of more than 1 level of indirection. Example taken from [1]

    const int **pp2;
    int *p1;
    const int n = 13;
 
    pp2 = &p1; /* not allowed, but suppose it were */
    *pp2 = &n; /** valid, both const, but sets p1 to point at n */
    *p1 = 10;  /* valid, but changes const n */
[1] https://www.sanfoundry.com/c-tutorials-concept-pointer-compa...

Re: Show HN: Fo: An experimental language which adds generics on top of Go

#95

Earlier quoted context omitted.

Exceptions are part of the reason that RAII is so critical in the C++ world. I don’t think that’s quite true. You definitely want RAII with exceptions, yes, but RAII is extremely useful even without exceptions. I believe it’s common to disable C++ exceptions but still use RAII. If you have multiple returns from a function (which can very easily happen with manual error checking) RAII is a big win.

> You definitely want RAII with exceptions, yes, but RAII is extremely useful even without exceptions. Which is another way of saying exceptions are part of the reason for RAII.

Yesssss, but, I still think that’s misleading. It’s like saying ATMs are part of the reason for cash.

Re: Show HN: Fo: An experimental language which adds generics on top of Go

#96
post #75

Earlier quoted context omitted.

Try implementing a shared map type where access is protected by a mutex and otherwise works just like Go's builtin map type (which is a generic map type).

Why would I need to re-implement what is already there?

If you want a more motivating example, try implementing generic array operations such as reduce, scan, each, reverse etc. Ideally these should be as easy to use as this example:

  x := reduce(func(a, b int)int{return a*b}, []int[1,2,3])
  y := reduce(func(a, b string)string{return a+b}, []string{"a", "b", "c"})
Rob Pike implemented "ivy", an array programming language, in Go but producing something like an array programming package that can be used from Go (sort of like NumPy for Pyhton) can be very messy to use or implement (even with the use of reflect package).

Re: Show HN: Fo: An experimental language which adds generics on top of Go

#97

Earlier quoted context omitted.

Go can statically type check hash maps, arrays, and channels. If you want some other container type it must be written with a concrete element type in mind.

Wait, really? The language designers gave themselves (effective) generics and then kicked down the ladder afterwards so that other container implementers couldn't follow? That's hilarious.

Why does it need to be fair? Serious question.

Re: Show HN: Fo: An experimental language which adds generics on top of Go

#98

Earlier quoted context omitted.

I've been working full time in Go since 2012. I've never had a use for generics.

And I've read about many people using Go and wishing for generics from day one.

It's mostly a "day 1" concern, is all.

Once you get over it you find that you can do whatever you need.

Re: Show HN: Fo: An experimental language which adds generics on top of Go

#99
post #97

Earlier quoted context omitted.

Wait, really? The language designers gave themselves (effective) generics and then kicked down the ladder afterwards so that other container implementers couldn't follow? That's hilarious.

Why does it need to be fair? Serious question.

It doesn't, obviously, it's just hypocritical and patronizing.

Re: Show HN: Fo: An experimental language which adds generics on top of Go

#100

Earlier quoted context omitted.

Image allows you to do this more or less. The problem to be solved here is that if you write the resize logic just once, you are accessing pixels from the image via the interface in a tight loop, and most of the time taken by your image resizer is the overhead of that access.

Interesting, you have a point actually. I would have thought that the performance cost of calling through an interface would be negligible, but I'm wrong about that: https://github.com/golang/go/issues/20116 The issues linked at the end there are interesting reads, and maybe they'll do something about this by Go 1.11. Having said all that, will an implementation of your code with generics be all that much faster? Or…

> Having said all that, will an implementation of your code with generics be all that much faster?

Likely yes. If generics are implemented via monomorphization, then you can avoid the overhead of virtual calls necessitated by interfaces.

It is possible that the compiler could become smart enough to devirtualize calls through interfaces.

Post reply on HN