How to tell Processing that the value is undefined?

How to tell Processing something like this: if the value is defined then do this, but if the value is undefined then do that ?

Tagged:

Answers

  • edited March 2017

    What value, specifically? Is this a Processing built-in object like PVector, or your own custom class object, or a string...? It depends -- for example, data primitives like int have a default value when they are declared, even if they are not initialized, so the approach you are describing wouldn't work for an int.

    int i;
    println(i): // prints 0
    
  • Sorry, maybe I got to write some code example, and then ask this question again, because now even I don't understand what I really meant : )

  • Answer ✓

    using null for undefined objects is a common thing in java - you often see snippets of code like

    if (str != null) {
      println("Length:", str.length());
    }
    

    mostly this is to protect calling methods on a undefined object, which will cause a null pointer exception.

    (this doesn't work for primitive types like int or float, but does for Objects like String, or indeed, Integer)

    • In Java by default, everything is defined before compilation.
    • Although we can use reflection techniques in order to check whether some particular member exists in an object.
    • However, we can also dynamically add & remove members from an object if we use some script language module.
  • Thanks, maybe "null" is what i needed. It is sad that not working with int and float. What is the difference between int and Integer? I don't even see Integer in processing reference category.

  • edited March 2017
  • edited March 2017

    For many designs it not necessary to use Integer rather than int just to have something nullable. You also have other options:

    1. Assign value(s) to an array or ArrayList or IntList, and check the length of the list to see whether a particular value has been assigned.
    2. For a single value, use a separate boolean as a guard field (e.g. isDefined).
    3. Depending on the use of the variable, use a natural value in the primitive itself to indicate non-assignment. For example, if you are only using the positive range of a signed int, -1 could mean "unassigned." Or use a non-operative value -- for example, if your int is a scaling factor, 1 does nothing; if your int is additive, 0 does nothing.
  • 3) Depending on the use of the variable, use a natural value in the primitive itself to indicate non-assignment.

    I've done that already. My fav value for it is MIN_INT. $-)

  • many useful tips, thanks, people.

  • I suspect that this is only true for javascript, but Daniel Shiffman is often blown away by how good javascript is to just turn everything into true or false.

    I bet that in javascript you can have an undefined or defined var "man" and ask if(man) {//do something}, and it will automatically conside whether or not "man" is undefined or defined even if it holds an integer or a float.

    But don't quote me on that, you'll need to try it. It might also work with java.

Sign In or Register to comment.