Why were coroutines removed from the beta?

Discuss the future of the AutoHotkey language
iseahound
Posts: 1472
Joined: 13 Aug 2016, 21:04
Contact:

Why were coroutines removed from the beta?

Post by iseahound » 09 Oct 2022, 11:45

I had a suspicion that something changed since the alpha release. It seems that coroutines were removed meaning that the output of a for-loop is now read only. It was really neat that they were formerly read/write, which meant that the generators also known as semi coroutines, were elevated to coroutines, by providing a communication channel not just from the enumerator function to the user, but a second channel from the user to the enumerator. Using the byref output variables makes it a lot easier and avoids the use of other languages like python which use generator.send() to accomplish the same thing.

Note: You must press "YES" when it says the variable is unset

Code: Select all

#Requires AutoHotkey v2.0-a128
#singleinstance force

nat() {
   ; Constants
   start := 1

   enumerate(&n) {
      MsgBox "Sent by user: " n
      n := start  ; yield statement
      start += 1  ; do block
      return True ; continue?
   }
   return enumerate
}

for n in nat() {
    MsgBox "Received from coroutine: " n
    n := A_Index * 2
}
The above code does not work on the current beta which is version 10.

Some helpful links:
Download link to a128 - https://www.autohotkey.com/download/2.0/AutoHotkey_2.0-a128-f63b0eb4.zip
An explanation of python's generator.send() - https://stackoverflow.com/a/19302700

EDIT: The exact version in which it was removed was beta.4, confirmed working on beta.3
beta 3 - https://www.autohotkey.com/download/2.0/AutoHotkey_2.0-beta.3.zip
beta 4 - https://www.autohotkey.com/download/2.0/AutoHotkey_2.0-beta.4.zip

beta.4 changelog - viewtopic.php?f=24&p=485902#p466061
Last edited by iseahound on 09 Oct 2022, 19:12, edited 4 times in total.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Why were coroutines removed from the beta?

Post by swagfag » 09 Oct 2022, 12:35

Code: Select all

nat() {
   ; Constants
   start := 1

   enumerate(&n) {
      MsgBox "Sent by user: " start
      n := &start  ; yield statement
      start += 1  ; do block
      return True ; continue?
   }
   return enumerate
}

for n in nat() {
    MsgBox "Received from coroutine: " %n%
    %n% := A_Index * 2
}
iseahound
Posts: 1472
Joined: 13 Aug 2016, 21:04
Contact:

Re: Why were coroutines removed from the beta?

Post by iseahound » 09 Oct 2022, 13:10

That changes the syntax, and it's better to return an object with a ToString() representation, and do n.set(42) or n.value := 42 because it's unintuitive to dereference the value output by a for loop
Post Reply

Return to “AutoHotkey Development”