0

What's wrong if this code, I just want to put a procedure inside of the record:

unit unTEFTipos;

interface

type

  TTEFPagamento = record
    AcrescimoDesconto: Double;
    TrocoSaque: Double;
    procedure Clear;
  end;

implementation

procedure TTEFPagamento.Clear;
begin
  AcrescimoDesconto := 0;
  TrocoSaque := 0;
end;

end.

But the Delphi 7 IDE is returning this errors:

 Build

 [Error] unTEFTipos.pas(10): 'END' expected but 'PROCEDURE' found  
 [Error] unTEFTipos.pas(11): 'IMPLEMENTATION' expected but ';' found  
 [Error] unTEFTipos.pas(13): '.' expected but 'IMPLEMENTATION' found  
 [Error] unTEFTipos.pas(10): Unsatisfied forward or external declaration: 'Clear'  
 [Fatal Error] GP.dpr(486): Could not compile used unit 'TEF\unTEFTipos.pas'
J...
  • 30,968
  • 6
  • 66
  • 143
Jack
  • 57
  • 1
  • 6
  • 6
    Delphi 7 does not allow record methods, such syntax was introduced in D2006. – MBo Apr 17 '18 at 13:03
  • While I voted to reopen after the edit, it's probably appropriate to point out [this question](https://stackoverflow.com/q/8460037/62576) and it's answer. Simply put, you're trying to use language features that didn't exist in D7. If you want to be able to use modern features, upgrade to a modern version of Delphi. The only way to do what you want in D7 is to use a class instead of a record. – Ken White Apr 17 '18 at 16:55
  • In older versions of Delphi, you have to use `object` instead of `record` if you want to add methods. – Arnaud Bouchez Apr 17 '18 at 17:22

1 Answers1

3

In older versions of Delphi, you have to use object instead of record if you want to add methods.

 TTEFPagamento = object
    AcrescimoDesconto: Double;
    TrocoSaque: Double;
    procedure Clear;
  end;

It will be compatible with newer versions too, even if you may face some problems when initializing managed variables within it.

So that I finally end up with writing something like:

 TTEFPagamento = {$ifdef UNICODE}record{$else}object{$endif}
    AcrescimoDesconto: Double;
    TrocoSaque: Double;
    procedure Clear;
  end;

which compiles on all versions, and behave the same.

Arnaud Bouchez
  • 42,305
  • 3
  • 71
  • 159
  • 1
    It would be good to include the specific version (D2006) where advanced record features were introduced. Unicode is a poor selector since advanced record support was available in D2006 and D2007, both non-unicode versions. It would also be good to discuss assignment semantics of `object` vs `record`. – J... Apr 17 '18 at 17:34
  • @J... It is not the point. It is not about the syntax feature, but about a compiler bug/regression. Unicode is a good selector for the bug/regression we need to circumvent - see http://blog.synopse.info/post/2011/01/29/record-and-object-issue-in-Delphi-2010 AFAICT this bug has never been fixed, since Embarcadero said `object` is deprecated. It may be from their point of view, but for people maintaining code compatible with oldest version of Delphi, e.g. a library or a set of projects with legacy code, this is a regression and need to be circumvent. – Arnaud Bouchez Apr 17 '18 at 20:21