Hacker News new | ask | show | jobs
by hazzen 5818 days ago
An immediate issue with the article: the author takes issue with the liberal attitude taken by C++ in adding features and then proceeds to state:

  Maybe you like [lambdas], maybe you don't, maybe you think
  they're God's gift to programming and any language
  without them is an infidel. But adding them would be
  harmless, anyway.
Well I agree on this point, it seems like a poor argument. You can't just declare a feature harmless if you ignore it. You make lambdas first-class and you end up baking them into libraries. Now you must use them.

Otherwise no issues, but it does feel like the author is advocating C++0x minus heavy template metaprogramming - which is a perfectly fine language in my book. Why not just use that and get on with life?

1 comments

If your language isn't completely terrible, then you can choose to use a named function anywhere an anonymous function will do. The libraries can never force you to not give your functions a name or not put them in a global context. That's why it's a harmless feature.

Templates might have been harmless if there was a way to do basic obvious stuff (callback functions, strings) without using them.

I'll hazard the guess that hazzen is interpreting 'lambdas' to involve closures.
Yes and no. If a library assumes a lambda is easy, you will see a lot of code like this (using somewhat bastardized OCaml types):

  interface 'a collection {
    void sort(lt:'a -> 'a -> bool);
  }
So, if you want to use lambdas, you would get:

  foos.sort(fn x y -> x.bar() > y.bar());
And if you don't want to use lambdas, you would get:

  bool sort_by_bar_gt(x:Foo, y:Foo) {
    return x.bar() > y.bar();
  }

  foos.sort(sort_by_bar_gt);
And that is assuming you can nest functions, but chances are you will have to put that function somewhere removed from the actual call to sort. This is the exact problem the STL hits: it assumes, for many things, you want a functional style - and then it doesn't give you a way of writing lambdas. You are left with one-off functors littering your code, wishing you could write that lambda.

I would like to stress: I think lambdas are a requirement for any language, I just take issue with the argument presented to convince lambda haters.

Since no GC is a given, a lambda with a closure maps nicely to the C idiom of a function pointer and a void* user data parameter.