Nice TWiki > Doc > NiceTutorial > OptionTypes TWiki webs:
Dev | Doc | Main | TWiki | Sandbox
Doc . { Changes | Index | Search | Go }
In Java, every non-primitive type can contain the special value null. However, calling a method on null will throw a NullPointerException at runtime. For instance, the following method compiles without warning, but fails at runtime if it is passed the null value.
void process(String s)
{
  int len = s.length();
  ...
}
Consequently, it is important to know if a method accepts the null value for each of its arguments, but Java offers no systematic way to declare that. It is a convention to specify it in the javadoc comments for the method. Therefore no automatic checking can be done by the compiler. This implies that any client that forgets to check the doc could make an illegal call without warning. It is also difficult to be sure that the comments are correct and to keep them up-to-date.

A more detailed review on safety issues can be found at http://nice.sourceforge.net/safety.html

For these reasons, types in Nice by default do not contain null. Luckilly, it is possible to extend them to allow it by prefixing them by ?. This allows methods to be documented, and the compiler can check that no error can occur. Of course, this means that the code has to test values that might be null if needed. For instance, the previous code would be written in Nice in a safe way. If the method does not accept the null value, the same code would compile, and a call using a possibly null value would be an error. If the method accepts the null value, the compiler will check that it handles that case correctly:

void process(?String s)
{
  if (s != null)
    {
      int len = s.length();
      ...
    }
  else
    ...
}

The compiler also takes into account what branches can be taken to infer the type information. For instance this code will also be accepted:

void process(?String s)
{
  if (s == null)
    return;

  // Here the compiler knows that s is not null
  int len = s.length();
  ...
}

Non-local variables

Testing for null values works only for local variables and parameters. It is not sufficient when accessing a field from an object. The reason for that is that the field value might be changed in a non obvious way between the test and the use. This includes modification by a concurrent thread and as a side effect of a method call. Consequently, it is necessary to copy the value of the field in a local variable, which can then be tested and used. Here are a few examples:
class A
{
  ?String name;
}

void process(A a)
{
  ?String name = a.name;
  if (name != null)
    {
      int len = name.length();
      ...
    }
  else
    ...
}

Special syntax

Array and function types have special syntax to make them allow the null value. For arrays, the ? goes between the brackets. For functions, it goes before ->. Here are a few examples:

String[] is a non-null array of non-null strings

?String[] is a non-null array of optional strings

String[?] is an optional array of non-null strings

?String[?] is an optional array of optional strings.

?String -> ?String is a function from optional strings to optional strings.

String ?-> String is an optional function from strings to strings.

?(String -> String) is equivalent to the previous type.

-- DanielBonniot - 17 Jun 2002

Topic OptionTypes . { Edit | Attach | Ref-By | Printable | Diffs | r1.3 | > | r1.2 | > | r1.1 | More }
Revision r1.3 - 14 Aug 2003 - 08:15 GMT - TWikiGuest
Parents: WebHome > NiceTutorial
Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback.