Undefined values: Difference between revisions

Content added Content deleted
(Add comment for Rust)
Line 1,405: Line 1,405:
In Smalltalk, all variables are automatically initialized to nil. This includes instance variables (an object's private slots), class variables and locals. Also global and namespace bindings can only be created by passing an existing value (typically also: nil) to the creation message (at:put:). This is part of the language's specification, not implementation specific. Thus a variable cannot ever be undefined.
In Smalltalk, all variables are automatically initialized to nil. This includes instance variables (an object's private slots), class variables and locals. Also global and namespace bindings can only be created by passing an existing value (typically also: nil) to the creation message (at:put:). This is part of the language's specification, not implementation specific. Thus a variable cannot ever be undefined.


Sending a message (aka performing a virtual function call) to the nil object results in an Exception, which can be caught if desired.
Sending a message (aka performing a virtual function call) to the nil object results in an exception, which can be caught if desired.
In other words, there is no segmentation violation or core dump, but well defined behavior in this situation.
In other words, there is no segmentation violation or core dump, but well defined behavior in this situation. Notice that nil is also an instance of a class, UndefinedObject (actually its only singleton instance). And that UndefinedObject does implement a few messages (for example: isNil, notNil, printOn: etc.)


However, we can check for the existence of a global binding with:
However, we can check for the existence of a global binding with: