Delphi programming language has all the guts to compete with C# and Java. For example, you can quite easily implement C# yield return in Delphi:
Example of yield return from C# reference:
public class PowersOf2 { static void Main() { // Display powers of 2 up to the exponent of 8: foreach (int i in Power(2, 8)) { Console.Write("{0} ", i); } } public static System.Collections.Generic.IEnumerable<int> Power(int number, int exponent) { int result = 1; for (int i = 0; i < exponent; i++) { result = result * number; yield return result; } } // Output: 2 4 8 16 32 64 128 256 }
Same code in Delphi:
program PowersOf2; {$APPTYPE CONSOLE} {$R *.res} uses System.Yield in 'System.Yield.pas'; function Power(Number: Integer; Exponent: Integer): TYieldEnumerable<Integer>; begin Result := TYieldEnumerable<Integer>.Create( procedure(Yield: TYield<Integer>) var RetVal: Integer; i: Integer; begin RetVal := 1; for i := 1 to Exponent do begin RetVal := RetVal * Number; Yield(RetVal); end; end); end; var i: Integer; begin for i in Power(2, 8) do begin Writeln(i); end; Readln; end.
System.Yield is a unit that I have created that implements the magic via Delphi enumerators and WinAPI fibers. Sure thing, people were implementing yield return in Delphi before, but they were doing this either via assembler by manipulating stack frames (problems with exceptions) or were bundling small yield return feature with giant libraries.
The unit of mine is very small (125+ lines of code), it does not have any other dependencies except WinAPI.Windows unit, it correctly handles exceptions and nested yields.
The only problem is that this unit is Windows only -- I am not aware if fibers API exists on MacOSX.
Attachment | Size |
---|---|
DelphiYield.zip | 6.55 KB |
Comments
Mark Kormann joins Estudio-Fera Company
My name is Mark. And I am a professional academic writer with many years of experience in writing.
My main focus is to solve problems related to writing. And I have been doing it for many years. I have been with several associations as a volunteer and have assisted in many ways.
My love for writing has no end. It is like the air we breathe, something I cherish with all my being. I am a passionate writer who started at an early age.
I’m happy that I`ve already sold several copies of my books in different countries like USA, Russia and others too numerous to mention.
I also work in a company that provides assistance to many clients from different parts of the world. Students always come to me because I work no matter how difficult their projects are. I help them to save time, because I feel fulfilled when people come to me for writing help.
Ghostwriter Writer – Mark Kormann - http://www.http://www.estudio-fera.com/ - Estudio-Fera Corp
Post new comment