SpencerPark / IJava

A Jupyter kernel for executing Java code.
MIT License
1.1k stars 215 forks source link

Mutually dependent classes not (really/always) working #123

Open wstomv opened 3 years ago

wstomv commented 3 years ago

How to define mutually dependent classes with the IJava kernel?

When I put them in the same code cell, it doesn't work. And when I put them in different cells, it doesn't always work, and needs multiple executions.

For example, the following code won't execute (compile) when put in a single cell (also not when switching the order of the class definitions):

class C {
    D d;

    C() { d = new D(this); }
}

class D {
    C c;

    D(C c) { this.c = c; }
}

You get an error message for class C:

 Unresolved dependencies:
   - class D

If you put them in separate cells, then executing the first cell (with class C) gives an error message:

Unresolved dependencies:
   - class D

Now, the second cell (with class D) can be executed without errors. And after that, also the first cell will execute, and both classes seem to work as expected.

You can also first execute class D, giving rise to the error message

Unresolved dependencies:
   - class C

after which class C executes, and then also class D executes.

Unfortunately, in my case the class definitions are a bit more involved, and this approach does not work.

Any suggestions for a workaround?

In a jshell this works, though you get a notification stating:

|  created class C, however, it cannot be referenced until class D is declared

I had hoped that IJava would behave similarly.

kyagrd commented 3 years ago

How to define mutually dependent classes with the IJava kernel? ... Any suggestions for a workaround?

I find this annoying too, There is a workaround though. Define empty classes in one cell first and then add their methods.