Hiya, I'm working on something where be able to pass functions around would be extremely useful. But I don't see anywhere if Java can do it? Can it?
Thanks for any help.
Addendum: I am aware of using interfaces, but I was curious if there was a more direct way to accomplish this.
Nope! Java doesn't have closures like that - Ruby does though! :D
I think that Java 7 was supposed to have them (Lambda expressions), but I've heard that it's now been delayed until Java 8.
So does c, but I was hoping to use java.... )-; Thanks
Originally Posted by quad64bit
I'm not sure I like the idea of including it - passing code blocks would be handy in some cases, but its dangerous and encourages lazy design IMHO. Java is one of those languages who's beauty has often been the product of its limitations - making it easy to pick up, and forceful in making you really design your system OOP style. Maybe not as much as ObjC, but its much simpler than ObjC too :D
True, but there are times where the usefulness. For instance cycling through a list of actions that an object has and passing it to someone.
Maybe you can think of a specific example where this wouldn't work, but I think this is usually accomplished using interfaces and passing objects to each other - allowing each object to implement its own actions, but all functioning under a global method call.
And they do, I'm more thinking of something like uh:
class obj extends BigObj// method list
And being able to just have an object pass an array of all the doSomethings to whoever need them. I could hard code it, but if an iteration of BigObj has a differing amount of doSomething()'s that need to be used, It would end up being a massive pain.
Yup, an interface could do it. You could pass an array of the interface to the object, and they could call the method on the interface objects.