mirror of
https://github.com/ParkerTenBroeck/coroutines.git
synced 2026-06-06 21:00:35 -04:00
27 lines
676 B
Java
27 lines
676 B
Java
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>{}
|
|
}
|