Recent

Author Topic: weird error message  (Read 355 times)

fcu

  • Full Member
  • ***
  • Posts: 131
weird error message
« on: July 11, 2026, 06:23:42 pm »
Code: Pascal  [Select][+][-]
  1. program test;
  2. {$mode objfpc}
  3. {$modeswitch advancedrecords}
  4.  
  5. type
  6.  trec = record
  7.   procedure proc; virtual;
  8.  end;
  9. begin
  10. end.
  11.  

when compiling this code you got a weird error message
main.pas(7,19) Error: Fields cannot appear after a method or property defini
tion, start a new visibility section first
main.pas(7,26) Fatal: Syntax error, ":" expected but ";" found
Fatal: Compilation aborted


we know virtual kayword is not allowed inside advanced records , why not just say : error : virtual is not allowed inside  advanced records

jamie

  • Hero Member
  • *****
  • Posts: 7902
Re: weird error message
« Reply #1 on: July 11, 2026, 06:37:01 pm »
I guess you picked the wrong data type, and yes, that is a oversight on error reporting.

This isn't C++ you know! :D

Jamie
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 19625
  • Glad to be alive.
Re: weird error message
« Reply #2 on: July 13, 2026, 07:22:39 am »
You do not implement Trec.proc so the compile will fail anyway.
I would expect a "forward delaration not solved."
Also, virtual methods are not allowed in records.
It should be something like this, that I tested in Delphi. (no fpc compiler available today, yet)
Code: Pascal  [Select][+][-]
  1. {$APPTYPE CONSOLE}
  2. {$ifdef fpc}{$mode delphi}{$endif}
  3. type
  4.  tmyrec = record
  5.   procedure myproc; //<-- no virtual. makes no sense in records.
  6.  end;
  7.  
  8.  procedure tmyrec.myproc; //<-- must implement
  9.  begin
  10.  end;
  11. begin
  12. end.
If that still fails it is a real bug.

BTW:
If you really want something like virtual in a record you must use a procedure variable of type myproc.
You can not override records, but you can change values from record fields.

« Last Edit: July 13, 2026, 07:37:01 am by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12641
  • Debugger - SynEdit - and more
    • wiki
Re: weird error message
« Reply #3 on: July 13, 2026, 07:34:14 am »
we know virtual kayword is not allowed inside advanced records , why not just say : error : virtual is not allowed inside  advanced records

virtual isn't a keyword. It's a modifier. Meaning: virtual is not reserved/protected. You can have variables/fields/functions called "virtual".

Thaddy

  • Hero Member
  • *****
  • Posts: 19625
  • Glad to be alive.
Re: weird error message
« Reply #4 on: July 13, 2026, 07:43:06 am »
I tested my corrected example on trunk and it fails. It is a bug apart from the two issues mentioned.
It compiles in Delphi 12.

Btw here's my suggestion:
Code: Pascal  [Select][+][-]
  1. {$ifdef fpc}{$mode objfpc}{$endif}
  2. {$apptype console}
  3. type
  4.  TMyproc = procedure;
  5.  
  6.  tmyrec = record
  7.   a:Tmyproc;
  8.  end;
  9.  
  10.  procedure myproc;
  11.  begin
  12.    writeln('myproc');
  13.  end;
  14.  
  15. var r:TMyrec;
  16. begin
  17.   r.a :=@myproc;
  18.   r.a;
  19.   readln;
  20. end.
Does not need advanced records too
« Last Edit: July 13, 2026, 07:47:11 am by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

Thaddy

  • Hero Member
  • *****
  • Posts: 19625
  • Glad to be alive.
Re: weird error message
« Reply #5 on: July 13, 2026, 07:53:17 am »
If the original layout is essential change record to object.
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. type
  3.  tmyobj = object
  4.   procedure myproc; virtual;
  5.  end;
  6.  
  7.  procedure TmyObj.myproc;
  8.  begin
  9.    writeln('myproc');
  10.  end;
  11.  
  12. var r:TMyObj;
  13. begin
  14.   r.myproc;
  15.   readln;
  16. end.
Objects semantics without using pointer types are otherwise almost  the same as records.
« Last Edit: July 13, 2026, 07:57:09 am by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

cdbc

  • Hero Member
  • *****
  • Posts: 2931
    • http://www.cdbc.dk
Re: weird error message
« Reply #6 on: July 13, 2026, 12:23:13 pm »
Hi
@Thaddy: +1
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

 

TinyPortal © 2005-2018