Recent

Author Topic: Const declaration  (Read 14531 times)

taazz

  • Hero Member
  • *****
  • Posts: 5368
Const declaration
« on: July 23, 2013, 10:36:41 am »
OK I'm seeing stars at the moment  %) so I'm going to try here and see if any one comes up with something. I want to declare an untyped constant of 20 bytes filled with 0 it must be untyped or what the FPC defines as ordinary constants for example

Code: [Select]
Type
  TMyTestArray = array[0..19] of byte;
const
  cMyConst :TMyTestArray = ($00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00)

is a typed constant that does what I want for an untyped would something like
Code: [Select]
const
  cMyConst = $00$00$00$00$00$00$00$00$00$00$00$00$00$00$00$00$00$00$00$00;

but that does not compute for the compiler. So how do you define an ordinal constant longer than the types supported from the compiler?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Const declaration
« Reply #1 on: July 23, 2013, 04:00:28 pm »
I want to declare an untyped constant of 20 bytes filled with 0 it must be untyped
I believe that the compiler assigns a type internally to all constants. It is just that 'untyped' ones are typed by the compiler rather than by the programmer. So whichever kind of constant declaration you use, you actually end up with a constant that has a type (even if it is not explicit). Which means that a 20-byte 'untyped' type would have to be based on a user-defined type such as you show for your typed constant byte array, since there is no native 20-byte type that I know of.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Const declaration
« Reply #2 on: July 23, 2013, 04:32:34 pm »
The difference are even dipper than simple having a type or not having a type for example do this

Code: [Select]
implementation

Const
  cTest1 = 'This is a string untyped const';
  cTest2 :String = 'This is a string typed const';
procedure Testing(Var1,var2:integer; Var3:string = cTest2);
begin
end;


compile and you get an error change the cTest2 in the procedure declaration to cTest1 and everything works. The problem is that typed constant are initialized variables which get their values at program start up instead of the untyped consts which get their value at compile time, that is done in order to support the {$J+} aka($WriteableConsts on/off) mode.

Its not a big deal really I just want to be able to use some of my typed constants as default values to procedure declarations instead of copying the contents around, which is not possible now with the typed consts. It helps with code maintenance.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Nebula

  • Jr. Member
  • **
  • Posts: 88
Re: Const declaration
« Reply #3 on: July 23, 2013, 09:29:53 pm »
When I was using Pascal in earnest 20 years ago, it was a case of remembering CTV (Cats Television - or any other way that floated your boat).

i.e. Consts first, then Types, then Variables. It followed that you couldn't declare a Type and then make a Const with it.

Has that all fallen by the wayside now?
Newbie testing Lazarus v1.0 - very impressed
Win 7 at work, XP and Linux Mint at home.
It all started with a ZX80 on a b/w telly........
Code: [Select]
Uses Smiles, GoodHumour, WantsToHelp;
{ never liked C - curly brackets are for comments! }

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Const declaration
« Reply #4 on: July 23, 2013, 09:44:01 pm »
Yes, personally I never liked that  rule it didn't made much sense. But even if I did follow it what stops me from declaring all my types in one unit and then use that unit in all other units? Rule still valid and followed custom types used.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Laksen

  • Hero Member
  • *****
  • Posts: 802
    • J-Software
Re: Const declaration
« Reply #5 on: July 23, 2013, 10:19:45 pm »
There are no arbitrary-length ordinal types in FreePascal. Ordinal types and strings are as far as I know the only ones that can appear as untyped constants.

So, no, you are out of luck :)

Why would you need such a thing to be untyped anyway?

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Const declaration
« Reply #6 on: July 23, 2013, 10:44:51 pm »
Here is a piece of code simplified to show case my frustration.
Code: [Select]
Type
  TMyExtremelyLongRecordOfIntegerOnlyfields = packed Record
    case integer of
      1: (W1,W2 ...Wn :Word);
      2:(DW1,DW2...DW(n/2):DWord);
      3:(DW1,DW2:DWord;W1,W2:Word; B1..B((n-6)*2):byte);
    end;
  end;
const
  cMyNullRec : TMyExtremelyLongRecordOfIntegerOnlyfields = (DW1 =0;DW2=0;..DW(n/2) =0);

So far so good, now considered a number of procedures that use that record as parameter and for some of them I can work with out the record as well so I have something like
Code: [Select]
procedure Proc1(var1,var2..VarN; MyRec:TMyExtremelyLongRecordOfIntegerOnlyfields = (dw1=0;dw2=0;......dw(n/2) = 0)

You get the picture I suppose. It would make my life easier if I didn't had to copy paste those pesky zeros around, I like the simplicity of just using a const for those cases and by changing that const I know that the rest of the code, that depends on it, will keep working as indented.

As I said it is not a big deal the usage count of that record's default value is less than 10 I was just trying to improve the code and remove any potential booby-traps for future development, I'll just change those cases to PMyExtremelyLongRecordOfIntegerOnlyfields and use the nil value for default that will have better effect than the const theory.

Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Const declaration
« Reply #7 on: July 28, 2013, 06:24:11 pm »
Well I just got kicked on my behind by this. I can't define a variant parameter with default value at all, any one knows if I would be able to do it in delphi mode at least?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1272
Re: Const declaration
« Reply #8 on: July 28, 2013, 08:08:53 pm »
This may be on complete the wrong tack, and I'm posting blind without a compiler in front of me to test.

Yesterday, I stumbled across a feature of FPC I hadn't heard of before.  Macros.
http://www.freepascal.org/docs-html/prog/progse4.html#x134-1350002.2

If you're just doing this to neaten code, could you define a macro?

{$MACRO ON}
{$DEFINE HACK_CONST:=(dw1=0;dw2=0;......dw(n/2)}

Then just just reference HACK_CONST where you need it?
« Last Edit: July 28, 2013, 08:31:25 pm by Mike.Cornflake »
Lazarus Trunk/FPC latest fixes on Windows 11
  I'm getting old and stale.  Slowly getting used to git, I'll get there...

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Const declaration
« Reply #9 on: July 28, 2013, 11:12:34 pm »
Quote
I can't define a variant parameter with default value at all, any one knows if I would be able to do it in delphi mode at least?
You can't do it in Delphi either, see the last paragraph:
http://docwiki.embarcadero.com/RADStudio/XE4/en/Parameters_(Delphi)#Default_Parameters

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Const declaration
« Reply #10 on: July 29, 2013, 01:57:35 am »
Instead of using default parameter, you can overload the procedure.
Code: [Select]
procedure LongFunc(para: TArrayType); overload;
begin
  // ...
end;

procedure LongFunc; overload;
begin
  LongFunc(LongFuncDefault);
end;

initialization

  LongFuncDefault:= ...

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Const declaration
« Reply #11 on: July 29, 2013, 03:10:27 pm »
Overloading is something I try to avoid mainly because it gets out of hand fairly quick with the compiler complaining that it can't decide which procedure to call, as for the delphi part I haven't checked I'm guessing that leledumbo is correct. The macros look promising if I get my head around them and re train my self to not jump off the chair when I see them a conditioning left on me from the days when I tried to learn c/C++ and failed obviously.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1272
Re: Const declaration
« Reply #12 on: July 29, 2013, 08:18:37 pm »
Quote
The macros look promising if I get my head around them and re train my self to not jump off the chair when I see them a conditioning left on me from the days when I tried to learn c/C++ and failed obviously.
I had the same reaction.  It's why I named the macro HACK_CONST :-)
Lazarus Trunk/FPC latest fixes on Windows 11
  I'm getting old and stale.  Slowly getting used to git, I'll get there...

 

TinyPortal © 2005-2018