Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 457
Himanshu Tanwar Avatar asked Jan 20 '26 16:01

Himanshu Tanwar


2 Answers

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.

like image 153
David Heffernan Avatar answered Jan 23 '26 04:01

David Heffernan


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.

like image 24
kipar Avatar answered Jan 23 '26 06:01

kipar