concept from Io language
- Posted by PL Feb 02, 2009
- 1085 views
Hello, I discovered euphoria sometime ago and find it interesting (specially, due to speed, co-routines, ...)
Concerning co-routines, I found a nice concept in language Io.
Let's give a small example:
V1 := O1 M1
This line execute method "M1" of object O1 and put result in variable V1. Nothing special ...
V1 := O1 @M1
Do the same but, - create a "future" (a special object) V1 - create a co-routine to execute the method asynchronously - when method is ended, V1 become an variable and get result of method
During execution of method, - the program can continue - If you try to access V1 (ex: V2 := V1 ...), program block untill M1 is finished. - You can of course check if M1 is finished
@M1 is indeed, sugar for @(M1) where @ is a function that execute a function in a co-routine.
You can for example, do sth like:
V1 := URL with("http://example1.com/") @fetch V2 := URL with("http://example2.com/") @fetch V3 := URL with("http://example3.com/") @fetch . . . do sth with V1 , V2 , V3
They give a nice example: a minimal webserver where each request is executed in a co-routine:
WebRequest := Object clone do( handleSocket := method(aSocket, aSocket streamReadNextChunk request := aSocket readBuffer betweenSeq("GET ", " HTTP") f := File with(request) if(f exists, f streamTo(aSocket), aSocket streamWrite("not found")) aSocket close ) ) WebServer := Server clone do( setPort(8000) handleSocket := method(aSocket, WebRequest clone **@**handleSocket(aSocket) ) ) WebServer start
Could that be implemented in euphoria ?