Recent

Author Topic: [CLOSED] Const vs define ?  (Read 567 times)

julkas

  • Hero Member
  • *****
  • Posts: 518
  • KISS principle / Lazarus 2.0.6 / FPC 3.0.4
[CLOSED] Const vs define ?
« on: December 09, 2019, 06:01:59 pm »
What is pros and cons of using const and define for conditional compilation?
When const and when define?
Thanks.
« Last Edit: December 10, 2019, 09:38:49 am by julkas »
procedure mulu64(a, b: QWORD; out clo, chi: QWORD); assembler;
asm
  mov rax, a
  mov rdx, b
  mul rdx
  mov [clo], rax
  mov [chi], rdx
end;

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7801
Re: Const vs define ?
« Reply #1 on: December 09, 2019, 06:23:36 pm »
I use define for conditional compilation based on booleans.

I rarely add consts for conditional compilation.  Usually in that case you check consts that are already there with if defined etc.

lucamar

  • Hero Member
  • *****
  • Posts: 2255
Re: Const vs define ?
« Reply #2 on: December 09, 2019, 07:53:18 pm »
A rule of thumb is: use $define except when the symbol has some use beyond contional compilation; in that case use const. Rather simple really :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.4/2.0.6  - FPC 3.0.4 on:
(K|L)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

julkas

  • Hero Member
  • *****
  • Posts: 518
  • KISS principle / Lazarus 2.0.6 / FPC 3.0.4
Re: Const vs define ?
« Reply #3 on: December 10, 2019, 06:07:45 pm »
A rule of thumb is: use $define except when the symbol has some use beyond contional compilation; in that case use const. Rather simple really :)
@lucamar how you will write following code with $define ?
Code: Pascal  [Select]
  1. const xsmart = 3;
  2. ...
  3. {$if defined(xsmart)}
  4. {$if xsmart < 4}
  5. ...
  6. {$elseif xsmart = 4}
  7. ...
  8. {$else}
  9. ...
  10. {$endif}
  11. {$else}
  12. ...
  13. {$endif}
  14.  
procedure mulu64(a, b: QWORD; out clo, chi: QWORD); assembler;
asm
  mov rax, a
  mov rdx, b
  mul rdx
  mov [clo], rax
  mov [chi], rdx
end;

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7801
Re: Const vs define ?
« Reply #4 on: December 10, 2019, 06:55:44 pm »
A rule of thumb is: use $define except when the symbol has some use beyond contional compilation; in that case use const. Rather simple really :)
@lucamar how you will write following code with $define ?
Code: Pascal  [Select]
  1. const xsmart = 3;
  2. ...
  3. {$if defined(xsmart)}
  4. {$if xsmart < 4}
  5. ...
  6. {$elseif xsmart = 4}
  7. ...
  8. {$else}
  9. ...
  10. {$endif}
  11. {$else}
  12. ...
  13. {$endif}
  14.  

A bit of fantasy please, not really that hard!

Code: Pascal  [Select]
  1.  
  2. {$macro on}
  3. {$define xsmart:=3}
  4.  
  5. begin
  6.  
  7. {$ifdef xsmart}
  8. {$if xsmart < 4}
  9. writeln('smaller');
  10. {$elseif xsmart = 4}
  11. writeln('equal');
  12. {$else}
  13. writeln('larger');
  14. {$endif}
  15. {$else}
  16. writeln('undefined');
  17. {$endif}
  18. end.
  19.  

julkas

  • Hero Member
  • *****
  • Posts: 518
  • KISS principle / Lazarus 2.0.6 / FPC 3.0.4
« Last Edit: December 10, 2019, 07:33:15 pm by julkas »
procedure mulu64(a, b: QWORD; out clo, chi: QWORD); assembler;
asm
  mov rax, a
  mov rdx, b
  mul rdx
  mov [clo], rax
  mov [chi], rdx
end;

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7801
Re: [CLOSED] Const vs define ?
« Reply #6 on: December 11, 2019, 12:50:57 pm »
@marcov Good.
https://stackoverflow.com/questions/1674032/static-const-vs-define-vs-enum

Questions like that don't really transfer languages well. Note that the answers cite C standard paragraphs which are obviously not valid for Pascal

julkas

  • Hero Member
  • *****
  • Posts: 518
  • KISS principle / Lazarus 2.0.6 / FPC 3.0.4
Re: [CLOSED] Const vs define ?
« Reply #7 on: December 11, 2019, 01:00:22 pm »
@marcov Good.
https://stackoverflow.com/questions/1674032/static-const-vs-define-vs-enum

Questions like that don't really transfer languages well. Note that the answers cite C standard paragraphs which are obviously not valid for Pascal
Link just for reading.
Pascal has const, define and enum also.
« Last Edit: December 11, 2019, 01:02:25 pm by julkas »
procedure mulu64(a, b: QWORD; out clo, chi: QWORD); assembler;
asm
  mov rax, a
  mov rdx, b
  mul rdx
  mov [clo], rax
  mov [chi], rdx
end;