mirror of
https://github.com/ParkerTenBroeck/coroutines.git
synced 2026-06-07 05:08:51 -04:00
organized, added futures
This commit is contained in:
parent
13d9ba7363
commit
6424548e75
8 changed files with 29 additions and 6 deletions
27
src/generator/gen/Gen.java
Normal file
27
src/generator/gen/Gen.java
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package generator.gen;
|
||||
|
||||
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)return (R)r.v;
|
||||
}
|
||||
}
|
||||
|
||||
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