Recent

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

julkas

  • Guest
[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 »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11351
  • FPC developer.
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: 4219
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/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

julkas

  • Guest
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.  

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11351
  • FPC developer.
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

  • Guest
« Last Edit: December 10, 2019, 07:33:15 pm by julkas »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11351
  • FPC developer.
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

  • Guest
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 »

 

TinyPortal © 2005-2018