AI 將 Delphi 程式碼移植至 C#、Java 或 C++ 時,6 種容易悄然改變的行為
6 Delphi behaviors that silently break when an AI ports your code to C#, Java or C++
Anton “Admiral” Fishman 列出 AI 將 Delphi 程式碼移植至 C#、Java 或 C++ 時容易出錯的 6 種行為,並以 Delphi 13 編譯器測試。
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 default, but Java's Math.round(2.5) and C++'s std::round(2.5) return 3. A port to Java or C++ that maps Round to the "obvious" function changes every halfway result.
2. A unit can replace Round
If the last unit in uses declares its own Round, an unqualified Round(2.5) calls that one, not System.Round. An agent that reads the call site alone will port the wrong function.
3. Strings start at 1
S[1] is the first character of a Delphi string. Off-by-one errors appear in every loop and Copy call that is translated literally to 0-based indexing.
4. Static arrays and records are copied on assignment
B := A copies a static array or a record by value; changing B leaves A alone. In C# and Java arrays are references, so the "same" code suddenly shares data. Dynamic arrays (array of T) behave the other way: they are shared until you call SetLength or Copy.
5. Interfaces count references — and can free your object
When an object that implements an interface is held both through an object variable and an interface variable, the object is destroyed as soon as the last interface reference goes away. The object variable is now dangling. In a garbage-collected target this whole class of bug disappears — and so does the behavior the original code depended on.
6. Extended is not the same type on every target
Extended is 10 bytes (80-bit x87) on Win32 and 8 bytes (an alias of Double) on Win64. The same source produces different floating-point results on the two targets, which matters when you compare a port against a 32-bit legacy build.
What I built
I turned these and many more behaviors into Delphi Behavior Contracts: a bilingual (EN/RU) reference of 74 chapters and 89 directive cards, measured on Delphi 13 (879 build observations, 650 runs on Win32 and Win64). Questions the documentation does not settle are listed by name instead of being guessed.
It installs as a Claude Code plugin, a plain skill for other agents, or an offline MCP server (it is in the official MCP Registry as io.github.DIIIUA/delphi-behavior-contracts).
- Free demo (sample chapters, working plugins and MCP): https://github.com/DIIIUA/delphi-behavior-contracts
- Full edition: https://silentfish.dev/delphi
If you port or maintain Delphi code, I would like to hear which traps your agents still fall into.
來源:DEV Community · dev.to