Passing multiple items to a using statement

You can pass one or multiple items in the using clause expression. You must separate each item by a comma character, not a semicolon character as in a typical Gosu statement list. You cannot pass more than one reentrant object, which is an object that implements IReentrant or IMonitorLock interfaces or has a lock method.

For example:

using ( _file1, _file2, _file3) { 
  // Do your main work here
}

To pass multiple reentrant objects, nest multiple using clauses, such as:

using ( lock1 ) {
  using ( lock2 ) {
  ...
  }
}

You can combine the multiple item feature with the ability to assign variables. Gosu runs any statements, including variable assignment, at run time and uses the result as an object to manage in the using clause. Within each comma-delimited assignment statement, you do not need a return statement.

For example:

using (var lfc = new FileInputStream(this).Channel,
       var rfc = new FileInputStream(that).Channel) {

  var lbuff = ByteBuffer.allocate(bufferSize)
  var rbuff = ByteBuffer.allocate(bufferSize)

  while (lfc.position() < lfc.size()) {
    lfc.read(lbuff)
    rfc.read(rbuff)

    if (not Arrays.equals(lbuff.array(), rbuff.array())) {
      return true
    }

    lbuff.clear()
    rbuff.clear()
  }
  return false
  }
}

Gosu ensures that all objects are properly cleaned up. Gosu cleans up only the objects that initialized without throwing exceptions or otherwise having errors such as returning null for a resource.

If you choose to initialize multiple resources and some but not all objects in the list successfully initialize, Gosu performs the following actions:

  1. Gosu skips initialization for any subsequent items not yet initialized, in other words the items that appear later in the initialization list.
  2. Gosu skips execution of the main part of the using clause.
  3. Gosu cleans up exactly the set of objects that initialized without errors. In other words, Gosu releases, closes, or disposes the object, depending on the type of object.

For example, suppose you try to acquire three resources, and the first one succeeds but the second one fails. Gosu does not attempt to acquire the third resource. Then, Gosu cleans up the one resource that did acquire successfully.

See also