简体   繁体   中英

Equivalent of yield return of C# in delphi

I am converting a C# code into Delphi 2010.I am stuck on finding an equivalent of yield return of C# in delphi. Can anyone help?

There is simply no equivalent. Delphi does not have any matching functionality. In C# yield return is used in C# iterator methods, and Delphi does not have anything remotely like C# iterator methods.

In Delphi you need to write iterators manually, without the syntactic sugar of iterator methods. This is described in the documentation .

There are at least two implementations of Yield in delphi (both not ideal, though).

First is using asm magic, here: http://santonov.blogspot.ru/2007/10/yield-you.html

Second is using Win32 fibers(very lightweight threads), so will work only on windows. It's here: http://www.gerixsoft.com/blog/delphi/yield

There is one more, slightly less readable, implementation based on fibers: http://delphisorcery.blogspot.ru/2011/04/yield-return-and-delphi.html

That said, i won't recommend using yield if you are new with Delphi, because it is not common way of programming in Delphi. So use it only if you understand consequences.

There is coroutine based framework for Delphi https://github.com/Purik/AIO

You can see example of generator (like in Python) here - https://github.com/Purik/AIO/blob/master/Demos/Tutorial/Generator.dpr

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM