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 › if ( value == null )
Page Index Toggle Pages: 1
if ( value == null ) (Read 1626 times)
if ( value == null )
Dec 2nd, 2009, 11:52am
 
When I try to use...

Code:
if ( value == null ) 



...because processing won't allow me to set default argument variables for a function, I get this error:

Code:
The operator != is undefined for the argument type(s) float, null 



I don't get it. 'value' is defined, and the whole idea of null is that it isn't defined either.

Can anyone help?
Re: if ( value == null )
Reply #1 - Dec 2nd, 2009, 12:17pm
 
only Classes can be null, and float is not a Class, it's a primitive type.
Re: if ( value == null )
Reply #2 - Dec 2nd, 2009, 12:41pm
 
Under what condition do you expect to see that your value is null? koogy is right, floats can't be null. null refers to a pointer pointing nowhere, instead of pointing to an object. Do you actually want to say if (value==0) instead? You should post more code.
Re: if ( value == null )
Reply #3 - Dec 2nd, 2009, 12:55pm
 
I wish to call a function which expect 2 values... but there are times when I don't want to pass any values and it to default to preset values.

In php I would do this...

Code:
void function(int x = 0, int y = 10)
{
 //code here
}


But it won't let me do this, so I thought I could just check if those values were null, like this...

Code:
void function(int x, int y)
{
 if ( x == null )
   x = 0;
 if ( y == null )
   y = 10;
}
Re: if ( value == null )
Reply #4 - Dec 2nd, 2009, 2:16pm
 
This might be bad style (I'm not a Java programmer either); but you could perhaps use overloaded functions:

Code:
void setup() {
size(100,100);
myFunction();
myFunction(5,3);
}

void draw() {


}

void myFunction() {
int x = 0;
int y = 10;
println(x + " " + y);
}

void myFunction(int x, int y) {
println(x + " " + y);
}


If your function is going to be more complex then have the overloaded functions call a separate function passing the appropriate parameters...
Re: if ( value == null )
Reply #5 - Dec 2nd, 2009, 2:33pm
 
Quote:
This might be bad style

No, it is a very classical idiom in Java. Not the most elegant, but well, people do that routinely.
Eg. in Processing code itself:
Code:
  public void colorMode(int mode) {
colorMode(mode, colorModeX, colorModeY, colorModeZ, colorModeA);
}

public void colorMode(int mode, float max) {
colorMode(mode, max, max, max, max);
}

public void colorMode(int mode, float maxX, float maxY, float maxZ) {
colorMode(mode, maxX, maxY, maxZ, colorModeA);
}

public void colorMode(int mode,
float maxX, float maxY, float maxZ, float maxA) {
// The complete code
}
Re: if ( value == null )
Reply #6 - Dec 3rd, 2009, 2:20am
 
liudr wrote on Dec 2nd, 2009, 12:41pm:
Under what condition do you expect to see that your value is null koogy is right, floats can't be null. null refers to a pointer pointing nowhere, instead of pointing to an object. Do you actually want to say if (value==0) instead You should post more code.


i did nearly suggest this but couldn't think of a decent value to suggest as a default - 0 is quite commonly a useful value for things 8)

there's also the problem of comparisons with floats as sometimes what you ask for and what you get are two different things because a float can't store the exact value.

the lack of defaultable parameters in java is one of my (many) bugbears.
Re: if ( value == null )
Reply #7 - Dec 3rd, 2009, 2:45am
 
Jon Cousins wrote on Dec 2nd, 2009, 12:55pm:
I thought I could just check if those values were null, like this...

Code:
void function(int x, int y)
{
 if ( x == null )
   x = 0;
 if ( y == null )
   y = 10;
}

Note 1: You can do that with void function(Integer x, Integer y), ie. encapsulating the int values in an object which then can hold the information "no data". It introduces a little overhead thought.
Note 2: Even if you do the above, it won't allow you to call function without parameters or with one parameter, Java compiler just won't allow that...
Re: if ( value == null )
Reply #8 - Dec 3rd, 2009, 7:20am
 
blindfish wrote on Dec 2nd, 2009, 2:16pm:
This might be bad style (I'm not a Java programmer either); but you could perhaps use overloaded functions:

Code:
void setup() {
size(100,100);
 myFunction();
 myFunction(5,3);
}

void draw() {

 
}

void myFunction() {
 int x = 0;
 int y = 10;
 println(x + " " + y);
}

void myFunction(int x, int y) {
 println(x + " " + y);
}


If your function is going to be more complex then have the overloaded functions call a separate function passing the appropriate parameters...


I would use this method as well. It increases a bit of overhead (double the code for two versions of a function, runtime overload may cost extra time). On the other hand, why not always supply the values even if the values are the default values? I use compile-time overload like above but you may get away by defining a macro (does java have macros or #define?) that replaces myFunction() with myFunction (default1, default2) at compile time. Like #define myFunction() myFunction (default1, default2);
Re: if ( value == null )
Reply #9 - Dec 3rd, 2009, 7:35am
 
No, duplicate code is terrible and you don't want that.

You would better go this way :

Code:
void myFunction(int x, int y) {
 println(x + " " + y);
}

void myFunction() {
 myFunction(0, 0);
}
Re: if ( value == null )
Reply #10 - Dec 3rd, 2009, 9:35am
 
antiplastik wrote on Dec 3rd, 2009, 7:35am:
No, duplicate code is terrible and you don't want that.

You would better go this way :

Code:
void myFunction(int x, int y) {
 println(x + " " + y);
}

void myFunction() {
 myFunction(0, 0);
}


That's a nice succinct way of handling it!  I certainly wasn't suggesting duplicating code - I was thinking more along the lines of:

Code:
void myFunction() {
 int x = 0;
 int y = 10;
 myComplexFunction(x,y);
}

void myFunction(int x, int y) {
 myComplexFunction(x,y);
}

void myComplexFunction(int x, int y) {
 // does stuff
}


...But of course if I'd actually typed that out I would have quickly realised that it was a little clunky and would have resorted to your approach (honest!)  Grin
Re: if ( value == null )
Reply #11 - Dec 3rd, 2009, 9:46am
 
Quote:
(does java have macros or #define?)

No.
Java programmers use overloading and polymorphism instead... :)
Re: if ( value == null )
Reply #12 - Dec 3rd, 2009, 9:50am
 
liudr wrote on Dec 3rd, 2009, 7:20am:
I would use this method as well. It increases a bit of overhead (double the code for two versions of a function, runtime overload may cost extra time).


@blindfish : I was punching liudr in the face, not you... but, mmm.. right, you wrote the code so you deserve your punch too ;-)   no hard feelings :-)
Page Index Toggle Pages: 1