Sign in Join

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

Mailing lists / lazarus / 2015-05

Declare record variant in it self

7 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

aradeonas

Hi,

I have a record and it has some value and a value with type of that record it self like this example but compiler wont allow me to do this and give "Type "TExampleRecord" is not completely defined" error,and I dont want to use class and keep creating and destroying it,they will be some simple record.

Show quoted text 7 lines
//Works with class TExample=class s:string; E:TExample; end; //Doesn't work with record TExampleRecord=record s:string; ER:TExampleRecord; end;

Regards, Ara

http://www.fastmail.com - mmm... Fastmail... -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150507/1d81e369/attachment-0001.html>

Reply by email

Michael Van Canneyt
to aradeonas
aradeonas wrote 18 lines
Hi, ? I have a record and it has some value and a value with type of that record it self like this example but compiler wont allow me to do this and give? "Type "TExampleRecord" is not completely defined" error,and I dont want to use class and keep creating and destroying it,they will be some simple record. ? //Works with class ? TExample=class ??? s:string; ??? E:TExample; ? end; ? //Doesn't work with record ? ? TExampleRecord=record ??? s:string; ??? ER:TExampleRecord; ? end;

You can never do this with records, it would recurse infinitely.

It works with a class, since a class is in fact just a pointer.

You must do

    PExampleRecord=^TExampleRecord;
  ? TExampleRecord=record
  ??? s:string;
  ??? ER:PExampleRecord;
  ? end;

and allocate the record on the heap.

Michael.

Reply by email

Mattias Gaertner
to aradeonas
Thanks Michael. Is there a way so I can use class with no need of create and destroy?So in this way I can even make simple functions in my class.

Maybe you mean something like this:

  TMyBaseRecord = object
    s: string;
  end;
  TMyRecord = object(TMyBaseRecord)
    ER: TMyBaseRecord;
  end;

Mattias

Reply by email

aradeonas
to Mattias Gaertner

Not exactly.I prefer to not make base for each record. Something like this :

Show quoted text 6 lines
TExample=class s:string; E:TExample; end; var e,e2:TExample; begin e.s:='1'; e2.s:='2'; e.E:=e2; Writeln(e2.s);

Regards, Ara

http://www.fastmail.com - IMAP accessible web-mail -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150507/a80a7e28/attachment.html>

Reply by email

Michael Van Canneyt
to aradeonas: Thanks Michael. Is there a way so I can use class with no need of create and destroy?So…
aradeonas wrote
Thanks Michael. Is there a way so I can use class with no need of create and destroy?So in this way I can even make simple functions in my class.

You can use Objects.
But you will need the same trick as for records, using a pointer.

Your definition is recursive.
There is no recursive definition in Pascal.

Mattias' proposal is the best you can do.

Michael.

Reply by email