Returning values from using clauses
You can return values from using clauses by using the standard
return statement. If you
return a value from a using
clause, Gosu considers the clause complete, and therefore calls your
object’s final life-cycle management method to clean up your resources:
- For disposable objects, Gosu calls the dispose method.
- For closable objects, Gosu calls the close method.
- For a reentrant or lock object, Gosu calls the exit method.
The following Gosu example opens a file using the
Java BufferedReader class
and reads lines from the file until the line matches a regular expression.
If code in the while loop
finds a match, it immediately returns the value and skips the rest of
the code within the using
clause.
uses java.io.File
uses java.io.BufferedReader
uses java.io.FileReader
function containsText(file : File, regExp : String) : boolean {
using (var reader = new BufferedReader(new FileReader(file))) {
var line = reader.readLine()
while (line != null) {
if (line.matches(regExp)) {
return true
}
line = reader.readLine() // Read the next line
}
}
return false
}
See also
