class::Nil
categories::Core
summary::Represents uninitialized data

description::

Nil has a single instance named nil and is used to represent uninitialized data,
bad values, or terminal values such as end-of-stream.

note::
Take note that Nil is a class and not the special value code::nil::. Comparing Nil and code::nil:: will return false.
In normal use you should not need to use this class directly: use code::nil::

code::
Nil.isNil; // false
Nil == nil; // false
Nil === nil; // false
::
::

instancemethods::

private::do, reverseDo, pairsDo, collect, select, reject, detect, collectAs, selectAs, rejectAs, pop, source, source_, changed, 	addDependant, removeDependant, release, update, swapThisGroup, performMsg, remove, seconds_, throw, superclassesDo, !?, play, printOn, storeOn, archiveAsCompileString, set, addDependant


method::isNil
Answers true because this is nil. In class link::Classes/Object:: this message is defined to answer false.
code::
[1, 2, nil, 3].collect(_.isNil);
::

method::notNil
Answer false. In class link::Classes/Object:: this message answers true.
code::
[1, 2, nil, 3].collect(_.notNil);
::

method::?
return first non-nil argument. Since this IS nil then return anObject.
In class link::Classes/Object::, ? is defined to answer the receiver.
code::
[1, 2, nil, 3].collect { |x| x ? -1 }; // replace nil by -1
::

method::??
If the receiver is nil, evaluate the function and return the result.  Since this IS nil, then evaluate the function and return the result. In class link::Classes/Object::, ?? is defined to answer the receiver.

code::
[nil, 2, nil, 3].collect { |x| x ?? { 100.rand } }; // replace nil by a random number
::

method::booleanValue
Returns false.
code::
[1, 2, nil, 3].collect(_.booleanValue);
// compare:
[true, false, false, true].collect(_.binaryValue);
::

method::rate
Returns nil.

method::numChannels
Returns nil.

method::isPlaying
Returns false.

method::dependants
Returns an empty IdentitySet.

method::awake
Returns nil.

method::nextTimeOnGrid
Returns clock.nextTimeOnGrid.

method::asQuant
Returns Quant.default.

method::matchItem
Returns true.

See also link::Reference/matchItem::.


code::
[3, 2, 1].select(nil.matchItem(_))); // returns all
// compare:
[3, 2, 1].select([1, -1, 2].matchItem(_))); // returns only those in the key collection
::


method::asCollection
Returns empty array.

method::get
Returns prevVal.


method::asSpec
Returns the default ControlSpec

method::handleError
Either report error or inspect error and halt execution.

method::push
Executes function.

method::appendStream
Returns stream.

subsection::Dependancy

All the messages for the Dependancy protocol (See class link::Classes/Object::) are defined in class Nil
to do nothing. This eliminates the need to check for nil when sending dependancy messages.

subsection::Other Methods

Many other messages are defined in class Nil to do nothing. This eliminates the need to check for nil.

subsection::Generic Collectors

There are a number of methods that can be applied to nil so that variables do not need to be initialized. Nil is just the "ground" (default case) from which the rest is bootstrapped.

method::add
Returns an array with the value. This makes it unnecessary to initialize when adding to a variable.
code::
x = nil;
x = x.add(8);  // returns an array
x = x.add(7); // appends to the array
::

method::addAll
Returns an array with all the values. This makes it unnecessary to initialize when adding to a variable.
code::
x = nil;
x = x.addAll([0, 2, 1, 2]);  // returns an array
x = x.addAll(7); // single objects are converted
::

method::remove
For nil, it just returns nil. This makes it unnecessary to initialize when removing from a variable and adding to it again.
code::
x = nil;
x.remove(1); // stays nil, returns nil
x = x.addAll([0, 2, 1, 2]);  // returns an array
x.remove(1); // returns 1
x;
::

method::++
Returns an array with all the values. This makes it unnecessary to initialize when adding to a variable.
code::
x = nil;
x = x ++ [7, 8, 9]; // returns the receiver
x = x ++ [3, 0, 1, 2]; // adds to the array
::

method::addFunc
Returns a function or a FunctionList.
This method is used to add multiple functions to already existing ones.
code::
f = nil;
f = f.addFunc { "----------****".scramble };
f = f.addFunc { 1.0.rand };
f.value;
::

method::removeFunc
This method is used to remove multiple functions from already existing ones. For Nil, it just returns itself.

code::
f = { 1.0.rand };
g = { "you have produced a random value".postln };
f = f.addFunc(g);
f.value;
f.removeFunc(g);
f.value;
::


method::transformEvent
This method is used to operate on events which are passed through the system as an argument.

code::
// for Nil: return the argument unmodified (an event).
nil.transformEvent((x: 8));
// for Dictionary (and thus for Event): add to the argument.
(y: 100, z: 1).transformEvent((x: 8));
// for Association: add the association to the event
(\a -> \x).transformEvent((x: 8));
// for Function: use the function receive the event as argument.
{ |event| event.use { ~x = ~x + 1 }; event }.transformEvent((x: 8));
::
