mirror of
https://github.com/ParkerTenBroeck/coroutines.git
synced 2026-06-07 05:08:51 -04:00
first
This commit is contained in:
commit
bce3396cf5
9 changed files with 695 additions and 0 deletions
28
src/generator/Gen.java
Normal file
28
src/generator/Gen.java
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
package generator;
|
||||
|
||||
public interface Gen<Y, R> {
|
||||
Res<Y, R> next();
|
||||
|
||||
static <Y, R> Gen<Y, R> yield(Y y) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
static <Y, R> Gen<Void, R> yield() {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
static <Y, R> Gen<Y, R> ret(R r) {throw new RuntimeException();}
|
||||
static <Y, R> Gen<Y, Void> ret() {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
default R await(){
|
||||
while(true){
|
||||
var res = next();
|
||||
if(res instanceof Ret(R r))return r;
|
||||
Gen.yield();
|
||||
}
|
||||
}
|
||||
|
||||
sealed interface Res<Y, R>{}
|
||||
record Yield<Y, R>(Y v) implements Res<Y, R>{}
|
||||
record Ret<Y, R>(R v) implements Res<Y, R>{}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue