Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-12

Having trouble defining an initialized constant correctly

2 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Cox, Stuart TRAN:EX

Please excuse me if I've posted this request to the wrong list. I really did spend some time in trying to find a FPC list for simple coding questions. Sorry.

I have a set of record types defined and want to create an initialized constant and an array constant with values. Occasionally I find that I just cannot get the structure of the constants written properly and the compiler whines at me until I throw my hands in the air. I'd really appreciate it if someone could show me what I'm not able to see for myself.

{The types are:}
Type
  TFloat = Double;
  TPoint2D       = record
     x,
     y:TFloat;
  end;
  TSegment2D      = array [1..2] of TPoint2D;
  TSlabel = string[5];
  TSegLabID = record
    slabel :    TSlabel;
    segmentNo : integer;
  end;
  TSrecord = record
    sSegLabID : TSegLabID;
    segment :   TSegment2D;
  end;
  TLSegment = record
    Srecord : TSrecord;
    trueLP : TPoint2D;
  end;
  TexistingLines5 = array[0..4] of TLSegment;

{Then the hard part:}

Const
  { one wee little one }
  LSegmentNull : TLSegment = (Srecord:(sSegLabID:(sLabel:'';
                                                  segmentNo:maxlongint)
                                      );
                              segment:((x:maxlongint;y:maxlongint),
                                       (x:maxlongint;y:maxlongint)));
                              trueLP:(x:maxlongint;y:maxlongint));
  { an array of five of the little blighters }
  CexistingLines : TexistingLines5= (

(Srecord:(sSegLabID:(sLabel:'0';segmentNo:0));segment:((x:0.5;y:0.5),(x: 3.5;y:1.7));trueLP:(x:0.5;y:0.5)),

(Srecord:(sSegLabID:(sLabel:'1';segmentNo:1));segment:((x:1.0;y:4.0),(x: 5.8;y:2.8));trueLP:(x:1.0;y:4.0)),

(Srecord:(sSegLabID:(sLabel:'2';segmentNo:2));segment:((x:1.5;y:3.0),(x: 6.2;y:2.4));trueLP:(x:1.5;y:3.0)),

(Srecord:(sSegLabID:(sLabel:'3';segmentNo:3));segment:((x:2.3;y:4.3),(x: 6.8;y:4.7));trueLP:(x:2.3;y:4.3)),

(Srecord:(sSegLabID:(sLabel:'4';segmentNo:4));segment:((x:2.5;y:0.3),(x: 6.8;y:4.7));trueLP:(x:2.5;y:0.3))
);

Can anyone save me from my misery?

Stu Cox

Reply by email

Sven Barth
to Cox, Stuart TRAN:EX
Please excuse me if I’ve posted this request to the wrong list. I really did spend some time in trying to find a FPC list for simple coding questions. Sorry.

Yes, I'd say you've found the correct list ^^

Show quoted text 6 lines
I have a set of record types defined and want to create an initializedconstant and anarrayconstant with values. OccasionallyI find thatI just cannot get the structure of the constantswritten properly and the compiler whines at me until I throw my hands in the air. I’d really appreciate it if someone could show me what I’m not able to see for myself.

To sum it up: you have to keep track to where you have to close your parenthesis (see below).

[snip]

Show quoted text 18 lines
{Then the hard part:} Const { one wee little one} LSegmentNull : TLSegment = (Srecord:(sSegLabID:(sLabel:''; segmentNo:maxlongint) ); segment:((x:maxlongint;y:maxlongint), (x:maxlongint;y:maxlongint))); trueLP:(x:maxlongint;y:maxlongint));

You end the "Srecord" after the "segmentNo" with the ");", but "segment" is also part of "Srecord". So put the ";" behind the closing parenthesis of "sSegLabID" and remove the ");" before "segment".

It should look like this (formatted a bit differently to highlight the end of a record a bit better):

LSegmentNull: TLSegment = (
Srecord: (
sSegLabID: (

       sLabel:'';
       segmentNo:maxlongint
       );
     segment: (
       (x:maxlongint; y:maxlongint),
       (x:maxlongint; y:maxlongint)
       )
   );
   trueLP: (
     x:maxlongint;
     y:maxlongint
   )

);

Show quoted text 17 lines
{ an array of five of the little blighters} CexistingLines : TexistingLines5= ( (Srecord:(sSegLabID:(sLabel:’0’;segmentNo:0));segment:((x:0.5;y:0.5),(x:3.5;y:1.7));trueLP:(x:0.5;y:0.5)), (Srecord:(sSegLabID:(sLabel:’1’;segmentNo:1));segment:((x:1.0;y:4.0),(x:5.8;y:2.8));trueLP:(x:1.0;y:4.0)), (Srecord:(sSegLabID:(sLabel:’2’;segmentNo:2));segment:((x:1.5;y:3.0),(x:6.2;y:2.4));trueLP:(x:1.5;y:3.0)), (Srecord:(sSegLabID:(sLabel:’3’;segmentNo:3));segment:((x:2.3;y:4.3),(x:6.8;y:4.7));trueLP:(x:2.3;y:4.3)), (Srecord:(sSegLabID:(sLabel:’4’;segmentNo:4));segment:((x:2.5;y:0.3),(x:6.8;y:4.7));trueLP:(x:2.5;y:0.3)) );

You have a similar problem here:

For one record ({!} marks the corrected points):

Srecord: (sSegLabID: (sLabel: '0';segmentNo:0); {!} segment:((x:0.5;y:0.5),(x:3.5;y:1.7))); {!} trueLP: (x:0.5;y:0.5)

Can anyone save me from my misery?

You should try to develop a formatting for such constant record definitions that allows you to spot parenthesis errors quickly.

Regards,
Sven

Reply by email