Sign in Join

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

Mailing lists / fpc-devel / 2016-04

Variant Records and Duplicate Identifiers

5 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

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

Unknown author

Hello,

I was reading the wiki page on records
(http://wiki.freepascal.org/Record), and I noticed the following quote: "The fields of the variable part are only available after assignment of its depending variable. It doesn't harm, whether [a particular field] is defined multiple times." However, when I tried to use this feature in some code, it refused to compile. In order to test the problem, I created the following example program, based on the example given on the wiki page.

=== begin code ===

program example;
uses sysutils;
type
   maritalStates = (single, married, widowed, divorced);
   TPerson = record
     name: record
       first, middle, last: string;
     end;
     sex: (male, female);
     dob: TDateTime;
     case maritalStatus: maritalStates of
       single: ( );
       married, widowed: (marriageDate: TDateTime);
       divorced: (marriageDate, divorceDate: TDateTime;
         isFirstDivorce: boolean)
   end;
var
   ExPerson: TPerson;
begin
ExPerson.name.first := 'John';
ExPerson.name.middle := 'Bob';
ExPerson.name.last := 'Smith';
ExPerson.sex := male;
ExPerson.dob := StrToDate('05/05/1990');
ExPerson.maritalStatus := married;
ExPerson.marriageDate := StrToDate('04/01/2015');
end.

=== end code ===

When I tried to compile that, I got the following errors:

=== begin error ===

$ fpc ex.pas
Free Pascal Compiler version 3.0.0 [2016/02/14] for x86_64 Copyright (c) 1993-2015 by Florian Klaempfl and others Target OS: Win64 for x64
Compiling ex.pas
ex.pas(19,18) Error: Duplicate identifier "marriageDate" ex.pas(21,3) Error: Duplicate identifier "marriageDate" ex.pas(35,4) Fatal: There were 2 errors compiling module, stopping Fatal: Compilation aborted
Error: C:\lazarus\fpc\3.0.0\bin\x86_64-win64\ppcx64.exe returned an error exitcode

=== end error ===

So the question then becomes: is the wiki in error, or is there something I'm missing? Furthermore, if the wiki is in fact wrong, and this is a limitation, is there any inherent reason why? It seems to me like this should be possible, it's certainly a very useful thing to have.

Thanks,
-Kevin

Reply by email

silvioprog
to Unknown author

On Thu, Apr 21, 2016 at 3:00 AM, wrote:
[...]

ex.pas(19,18) Error: Duplicate identifier "marriageDate" ex.pas(21,3) Error: Duplicate identifier "marriageDate"

[...]

Same problem on Delphi (Seattle):

"[dcc32 Error]: E2004 Identifier redeclared: 'marriageDate'".

Silvio Clécio

Reply by email

Maciej Izak
to Unknown author: I was reading the wiki page on records (http://wiki.freepascal.org/Record), and I noticed…

2016-04-21 8:00 GMT+02:00 :

Show quoted text 21 lines
type maritalStates = (single, married, widowed, divorced); TPerson = record name: record first, middle, last: string; end; sex: (male, female); dob: TDateTime; case maritalStatus: maritalStates of single: ( ); married, widowed: (marriageDate: TDateTime); divorced: (marriageDate, divorceDate: TDateTime; isFirstDivorce: boolean) end; So the question then becomes: is the wiki in error, or is there something I'm missing? Furthermore, if the wiki is in fact wrong, and this is a limitation, is there any inherent reason why? It seems to me like this should be possible, it's certainly a very useful thing to have.

That is 100% bad example. You can't have two fields with the same name even for that kind of record with "case" part. Correct example:

=== begin code ===
type
  TMaritalStates = (single, married, widowed, divorced);
  TPerson = record
    name: record
      first, middle, last: string;
    end;
    sex: (male, female);
    dob: TDateTime;
    case maritalStatus: TMaritalStates of
      single: ();
      married,widowed,divorced: (
        marriageDate: TDateTime;
        case TMaritalStates of
          widowed: (
            widowedDate: TDateTime);
          divorced: (
            divorceDate: TDateTime;
            isFirstDivorce: boolean)
        );
  end;
=== end code ===
Best regards, Maciej Izak

Reply by email

Sven Barth
to Maciej Izak
"Maciej Izak" wrote 20 lines
2016-04-21 8:00 GMT+02:00 : > > type > maritalStates = (single, married, widowed, divorced); > > TPerson = record > name: record > first, middle, last: string; > end; > sex: (male, female); > dob: TDateTime; > case maritalStatus: maritalStates of > single: ( ); > married, widowed: (marriageDate: TDateTime); > divorced: (marriageDate, divorceDate: TDateTime; > isFirstDivorce: boolean) > end; > > So the question then becomes: is the wiki in error, or is there

something I'm missing? Furthermore, if the wiki is in fact wrong, and this is a limitation, is there any inherent reason why? It seems to me like this should be possible, it's certainly a very useful thing to have.

That is 100% bad example. You can't have two fields with the same name

even for that kind of record with "case" part. Correct example:

You know that this was the point of the example? Cause the documentation seems to be a bit misleading there as Kevin wrote in his original mail.

Regards,
Sven

Reply by email

Maciej Izak
to Sven Barth

2016-04-22 10:50 GMT+02:00 Sven Barth :

You know that this was the point of the example? Cause the documentation seems to be a bit misleading there as Kevin wrote in his original mail.

No :). Now I know that. Thanks for clarification.

Best regards, Maciej Izak

Reply by email