Jump to content

Undefined values: Difference between revisions

→‎{{header|Haskell}}: Added solution for Delphi and C#
(→‎{{header|Haskell}}: Added solution for Delphi and C#)
Line 1:
{{task}}For languages which have an explicit notion of an undefined value, identify and exercise those language's mechanisms for identifying and manipulating a variable's value's status as being undefined
 
=={{header|CSharp}}==
When a basic type is suffixed by a question mark the type is marked nullable and gains an additional NULL value in addition to its usual allowed values.
<lang csharp>
int? answer = NULL;
if(NULL == answer) {
answer = 42;
}
</lang>
 
=={{header|Delphi}}==
Delphi and its dialects don't have an undefined notion for all variables, but implement the notion of the keyword nil that is untyped, yet compatible with all pointer types and object references as well as interfaces. No compatibility is given for non-referenced data types like integers, enums and records - as well as string types (Some exceptions exist due to some compiler magic for those types).
 
For Referenced data types like pointers, classes and interfaces a reference can be explicitely set to undefined by assigning the NIL value for it. No memory management like garbage collection (except for interfaces) is done.
<lang delphi>var
P: PInteger;
begin
New(P); //Allocate some memory
try
If Assigned(P) Then //...
begin
P^ := 42;
end;
finally
Dispose(P); //Release memory allocated by New
end;
end;</lang>
 
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|Haskell}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.