Details
-
Type:
Wish
-
Status:
Closed
-
Priority:
Major
-
Resolution: Won't Fix
-
Affects Version/s: None
-
Fix Version/s: None
-
Component/s: None
-
Labels:None
-
Number of attachments :
Description
Generators (like list comprehensions but without the list) rock. We need to add something like this to the language...
e.g.
Issue Links
- is depended upon by
-
GROOVY-762
implement syntax improvements
-
- relates to
-
GROOVY-2046
MethodClosure iterators could loop forever
-
-
GROOVY-4105
List Comprehensions are Missing from Groovy
-
Is anybody alive here? I tried to comment other issues but didn't get any answers
Comment for this issue: as far as I understand, generators must be executed in another stack (not current). The one way to create new stack in Java is to create Thread. So generators should be executed in another thread. So code like:
def f() {
{ yield factorial(i); }for (i: 0..3)
}
should be compiled to something like:
class Generator implements Iterator {
yieldValue;
next() {
{ returnValue = yieldValue; thread.notify(); // object has been read return returnValue; }if (!hasNext())
throw new NoSuchElementException();
synchronized (thread)
}
hasNext()
{ ... }class GeneratorThread extends Thread {
{ yieldValue = factorial(i); thread.wait(); // wait object has been read }synchornized run() {
for (i: 0..3)
}
}
GeneratorThread thread;
Generator()
{ thread = new GeneratorThread(); thread.start(); }}
def f() {
return new Generator();
}
Real code is harder to understand. I'll attach working Generator class, written in Java in 5 minutes.
I'm right? So I'd like to implements this in Groovy...