Computer Science Canada

Passing a method as a parameter

Author:  Raknarg [ Mon Nov 04, 2013 1:07 pm ]
Post subject:  Passing a method as a parameter

I was wondering if it was possible to set a parameter or variable as a method. In turing, for instance, you can do this:

Turing:

var thing : proc x

proc hi
   put "hi"
end hi

thing := hi


Or in Python there's a method called reduce whose first argument takes in a function. Is there anything like that in java?

Author:  Raknarg [ Mon Nov 04, 2013 1:58 pm ]
Post subject:  RE:Passing a method as a parameter

Oh I just discovered you can use "implement" to do sort of the same thing, you define a bunch of classes with one method that is the function you want.

Any other ideas out there?

Author:  rdrake [ Mon Nov 04, 2013 2:57 pm ]
Post subject:  Re: Passing a method as a parameter

Right now you can use an anonymous inner class. Once Java joins the future, you can also use lambda functions like in Python.

Author:  Raknarg [ Mon Nov 04, 2013 3:51 pm ]
Post subject:  RE:Passing a method as a parameter

Well technically speaking I'm not using Java, but something based off of Java (Processing, in case you're curious), so what I stated above works in it but I don't think I can do that


: