Scala-ARM (Automatic-Resource-Management) utilizza delimitato
import java.io._
import util.continuations._
import resource._
def each_line_from(r : BufferedReader) : String @suspendable =
shift { k =>
var line = r.readLine
while(line != null) {
k(line)
line = r.readLine
}
}
reset {
val server = managed(new ServerSocket(8007)) !
while(true) {
// This reset is not needed, however the below denotes a "flow" of execution that can be deferred.
// One can envision an asynchronous execuction model that would support the exact same semantics as below.
reset {
val connection = managed(server.accept) !
val output = managed(connection.getOutputStream) !
val input = managed(connection.getInputStream) !
val writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(output)))
val reader = new BufferedReader(new InputStreamReader(input))
writer.println(each_line_from(reader))
writer.flush()
}
}
}
una NIT: CPS (stile continuazione-passing) è qualcosa che è possibile in qualsiasi linguaggio che ha o può simulare funzioni di ordine superiore. Ad esempio, quando si passa un oggetto callback a un metodo in Java, questo è in realtà un esempio di CPS. Il plug-in * delimited continuations * di Scala fornisce un modo per scrivere strutture di controllo che somigliano sintatticamente allo "stile diretto" ma che sono trasformate in CPS dietro le quinte. –