Skip to content
Brian S. O'Neill edited this page Jun 7, 2022 · 6 revisions

Q: How can I define a mutually recursive function?

If function "a" calls function "b" which then calls function "a", then both of the functions must be declared before they are defined. Otherwise an IllegalStateException is thrown because the dependent function cannot be found.

    // Declare both functions before defining any code.
    MethodMaker mma = cm.addMethod(int.class, "a", int.class).static_();
    MethodMaker mmb = cm.addMethod(int.class, "b", int.class).static_();

    // Define code for "a", which invokes "b".
    ...
    mma.invoke("b", ...);

    // Define code for "b", which invokes "a".
    ...
    mmb.invoke("a", ...);

Note that in this example, the methods are declared static_ up front. This ensures that the correct method invocation instructions are generated.

Clone this wiki locally