3

I was creating some functions in Delphi 5, and accidentally I tried to compile without commas separating a list of parameters, and oddly it worked completely normal.

I tried isolating the issue as follows :

program Project1;
{$APPTYPE CONSOLE}

procedure foo(i : integer; s : string; di : integer = -1);
begin
  WriteLn(s);
end;

var
  str: string;
  int: integer;
begin
  str := 'aaa';
  int := 1000;
  foo(int str int);
  ReadLn;
end.

And it compiles, no exceptions are thrown and the paremeters reachers the functions completely as expected.

What I noticed is that if I remove the default parameter in the end, it start throwing compile error Not enough actual parameters

Why does Delphi has this behaviour? Is this some kind of a compiler bug or a weird mechanic instead?

J...
  • 30,968
  • 6
  • 66
  • 143
Rodolfo Donã Hosp
  • 1,037
  • 1
  • 11
  • 23
  • 4
    Delphi has a lot of weird syntax "features". I'd attribute it to bug, shrug, and move on. (for example, the allowed order of function decorating keywords like virtual, abstract, overload, inline, stdcall, static, ...) and the question, whether they require, tolerate or do not like a semi-colon in between them) – JensG Mar 01 '19 at 15:14
  • 4
    Compiler bug..... – David Heffernan Mar 01 '19 at 15:28

1 Answers1

5

This appears to be a compiler bug which has been fixed since at least D2010 and likely earlier. Delphi 5 is twenty years old as of this writing and default parameters were only introduced in D4 so it stands to reason that there were some early issues that have since been corrected.

J...
  • 30,968
  • 6
  • 66
  • 143
  • Thought about that, but it's odd that even with the bug, the compiler manages to direct the parameter values to it's correct location, since I supposed it searches for a comma – Rodolfo Donã Hosp Mar 01 '19 at 17:00
  • 4
    Parsers don't search for specific things. They use tokenisers. So this sort of bug is very understandable. – David Heffernan Mar 01 '19 at 17:04
  • I have a pretty shallow understanding of the process, but don't it look for tokens based on the presence of the specific separator, in this case, the comma? – Rodolfo Donã Hosp Mar 01 '19 at 17:07
  • 4
    It depends but your assumption that the parser searches for commas isn't how it works. There will be a loop that consumes tokens. And the bug will be something like the parser not detecting that commas were omitted. – David Heffernan Mar 01 '19 at 17:36
  • 1
    @RodolfoDonãHosp Default parameters were only introduced in D4. There were evidently bugs in the tokenizing routines added for this language feature. Only the devs can say what the underlying cause was, but it's not unusual for new language features to not quite come out right in the first iteration or two. – J... Mar 01 '19 at 17:59
  • Thank you guys for the clarification, it makes more sense now – Rodolfo Donã Hosp Mar 01 '19 at 18:10