[fpc-devel] Variant Records and Duplicate Identifiers
kevin at kdx.me
kevin at kdx.me
Thu Apr 21 08:00:23 CEST 2016
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
More information about the fpc-devel
mailing list