DISQUS

Life is grand: Functions as arguements

  • Brian Delahunty · 3 years ago
    You can do that is C++ using function pointers and it's the same as Delegates in C#... you can pass a function into a function as a paramater and it can then call it etc.

    I think Java has something similar.
  • Justin Mason · 3 years ago
    hi Paul --

    This is a reasonably common feature in dynamic languages like Perl, Python and Ruby.

    some of the things it allows are mindblowing; it gets even better when variable scoping is modified to allow "closures". You know the java pattern whereby you need to define an interface to receive a callback, then callers need to define instances of that interface etc. etc.? That becomes a single line of code in perl ;) it's truly beautiful in terms of simplification and readability.
  • Shog9 · 3 years ago
    What's even better is that you can return functions. Combined with closures, this results (for me) in a huge reduction in the need for big class hierarchies of simple classes, and drastically reduced the sort of cruft and namespace pollution that made me hate C++ so many years ago (a hatred that only diminished when i learned about templates).