Nil has a single instance named nil and is used to represent uninitialized data, bad values, or terminal values such as end-of-stream.
nil
. Comparing Nil and nil
will return false. In normal use you should not need to use this class directly: use nil
Nil.isNil; // false
Nil == nil; // false
Nil === nil; // false
Answers true because this is nil. In class Object this message is defined to answer false.
xxxxxxxxxx
[1, 2, nil, 3].collect(_.isNil);
Answer false. In class Object this message answers true.
xxxxxxxxxx
[1, 2, nil, 3].collect(_.notNil);
return first non-nil argument. Since this IS nil then return anObject. In class Object, ? is defined to answer the receiver.
xxxxxxxxxx
[1, 2, nil, 3].collect { |x| x ? -1 }; // replace nil by -1
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 Object, ?? is defined to answer the receiver.
xxxxxxxxxx
[nil, 2, nil, 3].collect { |x| x ?? { 100.rand } }; // replace nil by a random number
Returns false.
xxxxxxxxxx
[1, 2, nil, 3].collect(_.booleanValue);
// compare:
[true, false, false, true].collect(_.binaryValue);
Returns nil.
Returns nil.
Returns false.
Returns an empty IdentitySet.
Returns nil.
Returns clock.nextTimeOnGrid.
Returns Quant.default.
Returns true.
See also matchItem.
xxxxxxxxxx
[3, 2, 1].select(nil.matchItem(_))); // returns all
// compare:
[3, 2, 1].select([1, -1, 2].matchItem(_))); // returns only those in the key collection
Returns empty array.
Returns prevVal.
Returns the default ControlSpec
Either report error or inspect error and halt execution.
Executes function.
Returns stream.
All the messages for the Dependancy protocol (See class Object) are defined in class Nil to do nothing. This eliminates the need to check for nil when sending dependancy messages.
Many other messages are defined in class Nil to do nothing. This eliminates the need to check for nil.
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.
Returns an array with the value. This makes it unnecessary to initialize when adding to a variable.
xxxxxxxxxx
x = nil;
x = x.add(8); // returns an array
x = x.add(7); // appends to the array
Returns an array with all the values. This makes it unnecessary to initialize when adding to a variable.
xxxxxxxxxx
x = nil;
x = x.addAll([0, 2, 1, 2]); // returns an array
x = x.addAll(7); // single objects are converted
For nil, it just returns nil. This makes it unnecessary to initialize when removing from a variable and adding to it again.
xxxxxxxxxx
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;
Returns an array with all the values. This makes it unnecessary to initialize when adding to a variable.
xxxxxxxxxx
x = nil;
x = x ++ [7, 8, 9]; // returns the receiver
x = x ++ [3, 0, 1, 2]; // adds to the array
Returns a function or a FunctionList. This method is used to add multiple functions to already existing ones.
xxxxxxxxxx
f = nil;
f = f.addFunc { "----------****".scramble };
f = f.addFunc { 1.0.rand };
f.value;
This method is used to remove multiple functions from already existing ones. For Nil, it just returns itself.
xxxxxxxxxx
f = { 1.0.rand };
g = { "you have produced a random value".postln };
f = f.addFunc(g);
f.value;
f.removeFunc(g);
f.value;
This method is used to operate on events which are passed through the system as an argument.
xxxxxxxxxx
// 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));