The Delphi Bug List

Entry No.
451
Compiler - Can't compile
Functions returning small objects give erroneous compiling error message
1.02 2.01 3.0 3.01 3.02 4.0 4.01 4.02 4.03 5.0 5.01 6.0 6.01 6.02 Kylix 1.0
AbsentExistsExistsExistsExistsExistsExistsExistsExistsExistsExistsExistsExistsExistsExists
Description
Reported by Björn Sahlen; checked by Reinier Sterkenburg
The code below produces an error message ("Variable required"). When the size of the object is incresed (n>4), the error goes away.

This is a bug becasue:

  • The compiler acts diferent depending on the size of the object.
  • The compiler accepts the code if you use the with statement instead of the dot notation (see the code example).
  • The Automatic Code Completion will give you this option!
const  
  n = 4;           // 1 to 4 not ok.  >= 5 ok !

type
  obj= object
    b: array [1..n]  of byte;
    procedure proc;
  end;

procedure obj.proc;
begin  end;

function func: obj;
begin  end;

var  x: byte;
begin
  x:= func.b[1];         // ok
  with func  do proc;    // ok
  func.proc;             // Error: Variable required !!!
end.
Solution / workaround
The only acceptable workaround for me is to use a record and standard functions and procedures instead of an object. An other workaround is of course to make sure that the size of your objects are at least five bytes
User-contributed comments
Chris R. Timmons
30 Jun 2001  10:08 PM GMT
Not a bug. The example code is buggy, not Delphi.

Bug #1: An old-style "object" type is used. Use "class" instead:

type
obj = class(TObject)

Bug #2: function "func" doens't return an instance of obj. None of the code at the end of the example should work since there is no object instance to work on. Change func to:

function func : obj;
begin
Result := obj.Create;
end;

With those two fixes, the example code works fine in Delphi 5.01.
Jordan Russell
01 Jul 2001  07:06 PM GMT
Sorry, but the example code is not buggy. Your understanding of old-style objects is buggy. :)
Latest update of this entry: 2002-04-03

Post a comment on this bug


Index page
Delphi Bug List home page
The Delphi Bug Lists are presently maintained by Jordan Russell, who has taken over this task from Reinier Sterkenburg since August 2000.
All feedback is appreciated. See also the feedback section of the Delphi Bug List home page.