We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Java interface issue with Processing
Page Index Toggle Pages: 1
Java interface issue with Processing (Read 536 times)
Java interface issue with Processing
May 16th, 2008, 3:57pm
 
That's not a question, because I have solved the issue, but it is syntax related and I thought I should share the problem and found solutions.

I was bitten by a strange limitation in Processing.

I has to write several different but similar classes with various parameters, or even algorithms. So I made an interface, so the classes could implement the interface and I can pass them as method parameters with interface (generic) type.
I first made a plain interface with parameterless methods and a dummy implementation with nearly empty methods. It worked fine.
Later I found I should pass one of my classes as parameter type.
Example:
Code:
interface Foo
{
int Bar(Heart h);
}
class FooC implements Foo
{
int Bar(Heart h) { /* Use h; */ return 1; }
}

where Heart is one of my classes.
Ouch! I got an error:
Quote:
Semantic Error: The static type "Temporary_8040_9052$Foo" must use a qualified name to access the non-static member type "Temporary_8040_9052$Heart" of the enclosing type "Temporary_8040_9052".

Apparently, a interface is a static type. It is fine if it uses a type external to the PApplet (eg. color, PImage, or some external class) but doesn't like using an internal class.
Problem: I can't use a "qualified name" to access the class, as the name is dynamically generated. Perhaps we could use a special notation that would be substitued by the pre-processor...

A possible workaround is to put the interface in a .java file, but then I must also externalize the Heart class, thus pass the PApplet instance around, etc. Not very elegant in my mind, even if all I do there is to use the color type (replacing by long seems to work), and a little drawing code. And using some auto cast, apparently.
Here again, the pre-processor could do this automatically (adding a PApplet reference, passing it around, etc.) but it is far from obvious, I fear...

Perhaps the issue is already somewhere in the depths of Discourse, I admit I haven't searched.
OK, I just searched and found the same case: http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Syntax;action=display;num=1150747743;start=0#0

fry says the same thing as above, that the interface must go outside of the PApplet. But I am reluctant to write semi-Processing code in my class. Sad

I used another workaround, that I thought interesting to share. Not very elegant (purists will puke) but it works:
Code:
interface Foo
{
int Bar(Object oh);
}
class FooC implements Foo
{
int Bar(Object oh) { Heart h = (Heart)oh; /* Use h; */ return 1; }
}
Re: Java interface issue with Processing
Reply #1 - May 16th, 2008, 7:07pm
 
As far as I'm aware this is not entirely unique to processing, the issue is that in processing, all your pde files are joined together and wrapped into a PApplet class, so any interface is actually a nested interface within PApplet, and the rules of java are what cause the problems I believe.
Re: Java interface issue with Processing
Reply #2 - May 17th, 2008, 2:59pm
 
Indeed, fundamentally, it is a Java issue, but within the precise context on Processing and its way of handling declarations.
In a real Java project, I suppose nobody would make an internal interface.
I just wanted to point out this limitation and highlight some possible solutions.

It is not really a defect of the design of Processing. As fry points out, this sub-language aims mostly at beginners or simple projects. So stuff like interfaces and abstract classes might be a bit stretched.

Yet, people use Processing as a learning tool toward programming in general, and Java in particular.
And being able to promote code reuse and good OO ideas would be nice. But of course, there is no simple solution for this, as I point out.

I have a similar issue with the Processing classes I make, which I want to reuse in various sketches. But I will develop that in another thread.
Re: Java interface issue with Processing
Reply #3 - May 17th, 2008, 5:44pm
 
But the point is that at worst, we only share Java's limitation--we're not actually limiting anything in Processing itself. If people want to do more OOP, they still can (using the .java extension).

The way the Processing API is set up, you're not prevented from doing all of it the Java way, it's just that once you're using Processing a bit your expectations change (re: having the PApplet instance available in the background, etc.) Passing around a PApplet or PGraphics object is no different from using g.drawLine() inside any other Java method that passes like paint(Graphics g).
Page Index Toggle Pages: 1