disposeAndNil()

As its name suggests, the disposeAndNil() function provides a convenient way to dispose of an object and nil-ing the reference to it.

It uses the IDisposable pattern on .NET and Island and uses Closeable on Java. The function just sets the variable to nil on Cocoa as losing the reference there cleans up the class.

Syntax:

method disposeAndNil(var anObject: Object);
void disposeAndNil(ref Object anObject);
func disposeAndNil(_ inout anObject: AnyObject?)
void disposeAndNil(ref Object anObject);

Calling disposeAndNil(someObject) is equivalent to the following code:

if assigned(someObject) and (someObject is IDisposable) then
    someObject.Dispose();
someObject := nil;
if (someObject != null && someObject is IDisposable)
    someObject.Dispose();
someObject = null;
if someObject != nil && (someObject is IDisposable) {
    someObject.Dispose()
}
someObject : nil
if (someObject != null && someObject is IDisposable)
    someObject.Dispose();
someObject = null;