<div dir="auto">Last time the conversation arise, the conversation revolved around the fact that virtual threads can replace all use cases of continuation.<div dir="auto">After quite a bit of back and forth, the conversation just died.</div><div dir="auto"><br></div><div dir="auto">I'll try to find a link to the archive when I get home</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Aug 28, 2023, 13:15 Daniel Schmid <<a href="mailto:daniel@wwwmaster.at">daniel@wwwmaster.at</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>
<p>Hi Rémi,</p>
<p>While this change may simplify the implementation, it would
expose the Continuation to others (the Iterator can be accessed
clients which could then access the Continuation with instanceof
patternmatching or casting the Iterator).<br>
While Continuation is an internal JVM class, I could imagine it
becoming public API at some point (While it currently isn't, I
don't know what would happen in the future) so I'd rather not use
that approach unless required.<br>
Because of that, these could then call Continuation.run bypassing
the Iterator#hasNext/Iterator#next and it.</p>
<p>Besides, I don't see it being much simpler if the Iterator would
extend Continuation as this would just result in passing the
ContinuationScope and Runnable to the constructor of the iterator
instead of the Continuation itself.<br>
</p>
<p>Yours,<br>
Daniel<br>
</p>
<div>Am 28.08.2023 um 11:20 schrieb Remi
Forax:<br>
</div>
<blockquote type="cite">
<div style="font-family:arial,helvetica,sans-serif;font-size:12pt;color:#000000">
<div>Hi Daniel,<br>
</div>
<div>I've taken a look to your implementation, i think it can be
simpler,<br>
</div>
<div>you can inherits from Continuation, so you can create a
subclass of Continuation that implements Iterator with a field
to store the yielded value.</div>
<div><br>
</div>
<div>Rémi<br>
</div>
<div><br>
</div>
<hr id="m_-1572500297756818603zwchr">
<div>
<blockquote style="border-left:2px solid #1010ff;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt"><b>From:
</b>"Alan Bateman" <a href="mailto:Alan.Bateman@oracle.com" target="_blank" rel="noreferrer"><Alan.Bateman@oracle.com></a><br>
<b>To: </b>"Daniel Schmid" <a href="mailto:daniel@wwwmaster.at" target="_blank" rel="noreferrer"><daniel@wwwmaster.at></a><br>
<b>Cc: </b>"core-libs-dev"
<a href="mailto:core-libs-dev@openjdk.org" target="_blank" rel="noreferrer"><core-libs-dev@openjdk.org></a>, "jdk-dev"
<a href="mailto:jdk-dev@openjdk.org" target="_blank" rel="noreferrer"><jdk-dev@openjdk.org></a><br>
<b>Sent: </b>Monday, August 28, 2023 11:12:51 AM<br>
<b>Subject: </b>Re: yield return based on Coroutines<br>
</blockquote>
</div>
<div>
<blockquote style="border-left:2px solid #1010ff;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt"><br>
This looks fun! It's probably best to bring this to
loom-dev. In its archives you'll find several discussions
about generators as several people have been interested in
that topic. Even when thread confined, the main concern has
been that exotic control flow yields leads to surprising
behavior with many of the existing constructs, e.g. in your
example think about behavior with finally blocks,
try-with-resources, locks, ... when the iterator is not
fully consumed.<br>
<br>
-Alan<br>
<br>
<div>On 28/08/2023 09:43, Daniel
Schmid wrote:<br>
</div>
<blockquote>
<p>Hi,</p>
<p>After seeing the JVM Language Summit talk on
Continuations (<a href="https://www.youtube.com/watch?v=6nRS6UiN7X0" target="_blank" rel="noreferrer">https://www.youtube.com/watch?v=6nRS6UiN7X0</a>),
I thought about it being possible to implement something
like "yield return" in languages like C# (or "yield" in
Python) based on Continuations.<br>
Kotlin has implemented a similar feature as well: <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/-sequence-scope/yield.html" target="_blank" rel="noreferrer">https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/-sequence-scope/yield.html</a><br>
Now that Continuations are in the JDK, I feel like it
can be used as a good primitive and now is a good time
to start about thinking about adding something like this
as a Java feature or the libraries.</p>
<p>After my experiments and some discussion with another
developer named Peter Eastham (<a href="https://github.com/Crain-32" target="_blank" rel="noreferrer">https://github.com/Crain-32</a>),
I was able to come up with an
implementation/proof-of-concept allowing something like
the following:</p>
<pre>public static void main(String[] args) {
System.out.println("main thread: " + Thread.currentThread());
for (String s : Yielder.create(YieldReturnTest::someMethod)) {
System.out.println("Text: " + s);
}
}
private static void someMethod(Yielder<String> y) {
y.yield("Hello - " + Thread.currentThread());
System.out.println("between yields");
y.yield("World - " + Thread.currentThread());
for (String s : Yielder.create(YieldReturnTest::otherMethod)) {
y.yield("nested: " + s);
}
y.yield("bye - " + Thread.currentThread());
}
private static void otherMethod(Yielder<String> y) {
y.yield("it can");
y.yield("also be");
y.yield("nested");
}</pre>
<p>output:</p>
<pre>main thread: Thread[#1,main,5,main]
Text: Hello - Thread[#1,main,5,main]
between yields
Text: World - Thread[#1,main,5,main]
Text: nested: it can
Text: nested: also be
Text: nested: nested
Text: bye - Thread[#1,main,5,main]</pre>
<p>In this example, the method reference passed to the
Yielder.create method would be run in a Continuation
while y.yield would yield the Continuation and make the
value available to the iterator (next() calls
Continuation#run).<br>
</p>
<p>You can find a simple proof-of-concept of that here: <a href="https://github.com/danthe1st/ContinuationYieldReturn" target="_blank" rel="noreferrer">https://github.com/danthe1st/ContinuationYieldReturn</a><br>
</p>
<p>Would it be possible to add something like this to the
JDK libraries?<br>
I feel like it might be a useful addition to the JDK
libraries as it simplifies creating sequences a lot.</p>
<p>Originally, I thought about whether it might be a good
idea to add syntax for this but after building that
proof-of-concept, it looks like it would be sufficient
to add this to the libraries and using methods like this
seems pretty natural.<br>
One thing I am concerned with this approach (opposed to
an approach that involves changing syntax) is that it
would be possible that the method suddenly runs in a
different thread if the hasNext()/next()-calls of the
Iterator chang the thread they are used in at some
point. While Continuations allow this behaviour, it
might seem a weird to developers who don't know how
Continuations work.<br>
But aside from that issue with iterations switching
threads, this approach seems pretty natural to me.<br>
</p>
<p>Yours,<br>
Daniel<br>
</p>
</blockquote>
<br>
<br>
</blockquote>
</div>
</div>
</blockquote>
</div>
</blockquote></div>