6 Delphi behaviors that silently break when an AI ports your code to C#, Java or C++
AI agents are good at producing code that compiles. With Delphi, "compiles" and "behaves the same" are far apart. Here are six behaviors I keep seeing agents get wrong — each one is measured on the Delphi 13 compiler, not recalled from memory. 1. Round is banker's rounding Round(2.5) = 2 and Round(3.5) = 4 in Delphi: halfway cases go to the nearest even number. C#'s Math.Round does the same by…
When AI agents generate code in C#, Java, or C++ from Delphi, they often create issues due to subtle differences in behavior. These six main issues are observed when using the Delphi 13 compiler:
1. The Round function in Delphi rounds half values to the nearest even number, while C# and Java round half values to the nearest integer, and C++ returns 3 in these cases. Mapping Round to the wrong function can lead to incorrect results.
2. Delphi allows a unit to replace the built-in Round function. If an agent encounters an unqualified Round(2.5), it may call the user-defined version instead of the System.Round function, causing unexpected behavior.
3. Delphi strings are 1-based, meaning S[1] is the first character. Translating code to 0-based indexing (as in C# and Java) can cause off-by-one errors in loops and Copy calls.
4. Delphi copies static arrays and records by value when assigned, making them independent. However, in C# and Java, arrays are reference types, so assigning the same array shares data. Dynamic arrays (array of T) work differently in C#, where they are shared until SetLength or Copy is called.
5. Interfaces in Delphi count references and can cause an object to be freed when the last interface reference disappears. This can lead to dangling object variables in garbage-collected targets, whereas the original code may have depended on that behavior.
6. The Extended data type is not identical across targets. In 32-bit builds, Extended is 10 bytes (80-bit x87), while in 64-bit builds, it is an alias for Double (8 bytes). This difference can result in different floating-point results when porting between targets, which is crucial when comparing a port to a legacy build.
Rewritten by urgent.news; may contain errors — check the original.