Purely as an exercise at home, aimed to better understand some language basics, I tried to reimplement the Ord function, but I came across a problem.
In fact, the existing Ord function can accept arguments of a variety of different types (AnsiChar, Char, WideChar, Enumeration, Integer, Int64) and can return Integer or Int64.
I can’t figure out how to declare multiple versions of the same function.
How should this be coded in Delphi?
Ordcannot be coded in Delphi. Although you can use theoverloaddirective to write multiple functions with the same name, you cannot write theOrdfunction that way because it works for an arbitrary number of argument types without needing multiple definitions. (No matter how manyOrdoverloads you write, I can always come up with a type that your functions won’t accept but that the compiler’s will.)It works that way because of compiler magic. The compiler knows about
Ordand about all ordinal types in the program, so it performs the function’s actions in-line. Other compiler-magic functions includeLength(magic because it accepts arbitrary array types),Str(magic because it accepts width and precision modifiers), andReadLn(magic because it accepts an arbitrary number of parameters).