Jump to content

Undefined values: Difference between revisions

→‎E: new example
m (→‎{{header|Go}}: clean up. no substantial code changes.)
(→‎E: new example)
Line 139:
 
If P was a Class only the Assigned function would be available; in addition Dispose would have to be replaced by FreeAndNil or calling the .Free method of the instance. For Interfaces no such last call would be necessary as simple removal of the reference would be sufficient to trigger the Garbage Collector.
=={{header|E}}==
 
First, there is the <var>null</var> object, which is simply a predefined object which has no methods (other than those every object has). <var>null</var> is generally used as a default return value and as a “no value” marker. <var>null</var> is not included in all object types as in Java; you must explicitly add it (e.g. <code>var foo :nullOk[List]</code> may be a List or null).
 
There are also <em>broken references</em>. Broken references are generated as the results of failed eventual sends (asynchronous invocations), and also may be constructed using <em>Ref.broken(...)</em>. It is an error to call or send to a broken reference, but it may be passed around like any other value. Broken references may be thought of as “Due to unexpected circumstances, this part of the state of the program is missing; do not proceed.”
 
Both of the above are values, which may be passed around and stored in variables. On the other hand, there are also ways to have an “undefined” <em>variable.</em> (However, it is not possible for a program to refer to an <em>nonexistent</em> variable; that is a static error which will be reported at the beginning of evaluation.)
 
Each variable's behavior is defined by a <em>slot</em> object with <code>get</code> and <code>put</code> methods. If the slot throws when invoked, then a program may contain a reference to that variable, but not actually access it. A notable way for the slot to throw is for the slot to itself be a broken reference. This may occur when slots are being passed around as part of metaprogramming or export/import; it is also used in certain control structures. For example:
 
<lang e>if (foo == bar || (def baz := lookup(foo)) != null) {
...
}</lang>
 
The slot for <var>baz</var> is broken if the left side of the <code>||</code> was true and the right side was therefore not evaluated.
 
Ordinarily, the programmer need not think about broken slots, and only works with null or broken values.
 
=={{header|Go}}==
Go has six types for which nil is defined. Values of these six types can be tested against the predefined identifier nil [http://rosettacode.org/wiki/Undefined_values/Check_if_a_variable_is_defined#Go as shown] by task [http://rosettacode.org/wiki/Undefined_values/Check_if_a_variable_is_defined Undefined values/Check if a variable is defined].
Cookies help us deliver our services. By using our services, you agree to our use of cookies.