We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Is it possible to create an ArrayList of function calls? For example, I have
void step0(){}
void step1(){}
void step2(){}
(...)
void stepN(){}
Then to create an
ArrayList<void> myFxn = new ArrayList<void>();
myFxn.add(step0);
myFxn.add(step1);
myFxn.add(step2);
myFxn.add(stepN);
Then finally
void keyPressed(){
if(key>='a' && key<='z')
myFxn.get(key-'a');
}
The ArrayList has a list of functions I could access in an ordered manner. Just curious if this is something that can be implemented with current tools.
Kf
Answers
In Java 8, JS, Python, etc., YES! However, even though P3 is bundled w/ Java 8, the way the compilation is configured and also the incomplete pre-compiler makes the PDE incompatible w/ Java 8's latest features! =((
There's still hope. A quick dirty approach is to rely on undocumented function method(): *-:)
Thxs. Do you mind showing a piece of code or a link showing the concept please @GoToLoop.
Kf
It could be done with the current version of Processing (JAVA mode) but it is challenging and uses the Java Reflection API.
A more advanced technique is to create an
interface
orclass
as a wrapper for your custom functions:Of course, if we already know how many we're gonna need, a regular array offers a lesser boilerplate aesthetically code: :P
Interesting @GoToLoop. I will check if it works in my code. Thanks.
@Quark what is the Java Reflection API?
Kf
http://docs.Oracle.com/javase/8/docs/api/java/lang/reflect/package-summary.html#package.description
This discussion demonstrates how to use reflection to execute a method whose name is stored in a string
Interesting... Do you have any links that would help me in learning all this? Thanks in advance.
If you mean reflection you might start here