I'm trying to implement a Linq-alike for Go, mainly for practice but also
because I find Enumerables really easy to work with. Unfortunately, I have
hit several walls, all due to missing features in the language.
1. *Generics.* Yeah, I know Google doesn't like them. But how else are
you gonna define a function that takes a parameter of type T along with a
function that takes a T and returns a T? Answer: You can't. It must take an
interface{} and a function that takes an interface{} and returns an
interface{}. And then you can't call this function and pass in an int and a
function taking an int and returning an int. No, you have to write a
specifically non-type-safe function and pass *that* in, even if you had
a perfectly good int->int function kicking around. Madness!
2. *Overloading.* I decided to start by writing Ana, Cata and Bind, a la
MinLINQ. Unfortunately, due to the lack of generics, I need to write an
Ana() that deals with interface{}s, along with specialised versions for
ints, strings and floats. But... you can't have functions with the same
name and different parameters, so that's out too.
3. *Methods. *So, I figured I'd make each overload of Ana be a method on
the relevant type, but the compiler spits out "cannot define new methods on
non-local type int". So I can't do that either.
Don't get me wrong: It's great to see the number of modern native-compiled
languages exceed "1", but seriously guys, how's a fellow supposed to get
anything done without generics?
I want a function like this:
func Ana(seed T, condition func(T) bool, next func(T) T) FEnumerable<T> {
... code ...
}
Is that really so unreasonable? Are you seriously telling me that writing
out loops and such at the call site is better than having a reusable *
*cough** "class library"? I don't want to be declaring lists and slices all
over the place every time I want to query something. I want to simply say
"From(something).Where(filter).Select(transformation)". You can do that in
C++, C#, D, VB.NET and pretty much any other modern language. If you're
going to have types, you must also have generics. Period. Otherwise we're
just back in the dark ages casting everything back and forth, or writing
the same code over and over again.
Again, let me say that I'm not knocking Go specifically. I'm sick of being
locked into C++ whether I like it or not (yeah, I know there's D, but it's
riddled with bugs). But what use are "first-class functions" when you have
neither generics nor polymorphism? The whole point of first-class functions
is to plug specific *strongly-typed* sorting or filtering functionality
into an otherwise generic *untyped* process. In other words, the function
you're calling doesn't need to know or care what kind of objects its
dealing with, because you've provided a function object that can do the
type-specific bit of work. But here I can't, because a func(int)int won't
be accepted by a function expecting a func(interface{})interface{}.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.