Recent

Author Topic: Type "TMyType" is not completly defined  (Read 7257 times)

Shpend

  • Full Member
  • ***
  • Posts: 167
Type "TMyType" is not completly defined
« on: December 08, 2019, 02:08:49 am »
Hi all,

In short I would like to be able to do the following:

Code: Pascal  [Select][+][-]
  1. TMultiSet<T> = record
  2.        fField: byte;
  3.        value: TMultiSet<T>;
  4.      end;  
  5.  

//Error: Type "TMultiSet<T> " is not defined yet

Is there any chance of making this possible and if so, how am I able to work around

Thx in advance!

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: Type "TMyType" is not completly defined
« Reply #1 on: December 08, 2019, 02:17:53 am »
Hi all,

In short I would like to be able to do the following:

Code: Pascal  [Select][+][-]
  1. TMultiSet<T> = record
  2.        fField: byte;
  3.        value: TMultiSet<T>;
  4.      end;  
  5.  

//Error: Type "TMultiSet<T> " is not defined yet

Is there any chance of making this possible and if so, how am I able to work around

Thx in advance!
Answer to first question: making it possible ? the answer is no because that record definition is recursive and the compiler cannot determine at any time what the size of the record is.

Answer to second question: how to work around it: define a pointer to a TMultiSet<T> (presumably PMultiset) and use that as the type of the "value" field.

HTH.
« Last Edit: December 08, 2019, 02:22:15 am by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Shpend

  • Full Member
  • ***
  • Posts: 167
Re: Type "TMyType" is not completly defined
« Reply #2 on: December 08, 2019, 02:30:11 am »
Hi, @440bx :

I tried ur suggestion, with this:

Code: Pascal  [Select][+][-]
  1.  
  2.     generic PMultiSet<T> = ^TMultiSet<T>;
  3.  
  4.      generic TMultiSet<T> = record
  5.        index: byte;
  6.        value: PMultiSet<T>;
  7.      end;  
  8.  

sry im kinda new to generics and stuff would you mind showing me a code example, i get only type errors :/
« Last Edit: December 08, 2019, 02:36:37 am by Shpend »

Shpend

  • Full Member
  • ***
  • Posts: 167
Re: Type "TMyType" is not completly defined
« Reply #3 on: December 08, 2019, 02:37:35 am »
and btw i get (attachment)error when I declare (attachment)code:


440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: Type "TMyType" is not completly defined
« Reply #4 on: December 08, 2019, 02:37:43 am »
Hi, @440bx :

I tried ur suggestion, with this:

Code: Pascal  [Select][+][-]
  1.  
  2.     generic PMultiSet<T> = ^TMultiSet<T>;
  3.  
  4.      generic TMultiSet<T> = record
  5.        index: byte;
  6.        value: PMultiSet;
  7.      end;  
  8.  

sry im kinda new to generics and stuff would you mind showing me a code example, i get only type errors :/
I think that with the following change your declaration should work
Code: Pascal  [Select][+][-]
  1. PMultiSet = ^TMultiSet<T>;
and the field "value" should be declared as
Code: Pascal  [Select][+][-]
  1. value : PMultiSet;
« Last Edit: December 08, 2019, 02:39:44 am by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Shpend

  • Full Member
  • ***
  • Posts: 167
Re: Type "TMyType" is not completly defined
« Reply #5 on: December 08, 2019, 02:41:47 am »
I tried your example mate, but sadly I get also here a type error:
(attachment!)

And it actually is correct, since he cant know what <T> should mean without  the generic keyword and/or the brackets in PMultiSet<T> i would assume..

but did you see also the attachment above: it says:

"Error: Internal error: 20010001" or the like

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: Type "TMyType" is not completly defined
« Reply #6 on: December 08, 2019, 02:49:32 am »
I tried your example mate, but sadly I get also here a type error:
(attachment!)

And it actually is correct, since he cant know what <T> should mean without  the generic keyword and/or the brackets in PMultiSet<T> i would assume..

but did you see also the attachment above: it says:

"Error: Internal error: 20010001" or the like
The internal error should not be happening, that should probably be considered a bug in FPC but, that is a determination better left to the developers.

I know what you're trying to do and since I rarely use generics I'm not getting the syntax right.  I'll be back in a few minutes after I figure out the correct way.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Shpend

  • Full Member
  • ***
  • Posts: 167
Re: Type "TMyType" is not completly defined
« Reply #7 on: December 08, 2019, 02:59:47 am »
I would thank you alot, since i just cant get around it myself currently ..

Shpend

  • Full Member
  • ***
  • Posts: 167
Re: Type "TMyType" is not completly defined
« Reply #8 on: December 08, 2019, 03:29:50 am »
I have an inner feeling that this doesnt work right now in fpc, hopefully im wrong about this

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: Type "TMyType" is not completly defined
« Reply #9 on: December 08, 2019, 03:51:33 am »
I thought I knew the answer to your question but, I don't. 

The following does NOT work but, I believe it should work
Code: Pascal  [Select][+][-]
  1. type
  2.   // Error: Generics without specialization cannot be used as a type for a variable
  3.  
  4.   PMultiSet = ^TMultiSet;
  5.   generic TMultiSet<T> = record
  6.     next : PMultiSet;
  7.     data : T;
  8.   end;
  9.  
I don't think the pointer declaration should require the generic type to be specialized.

Hopefully, someone else can answer your question.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

bytebites

  • Hero Member
  • *****
  • Posts: 632
Re: Type "TMyType" is not completly defined
« Reply #10 on: December 08, 2019, 05:07:57 am »
Delphi mode
Code: Pascal  [Select][+][-]
  1.   TMultiSet<T> = record
  2.          fField: byte;
  3.          value: ^TMultiSet<T>;
  4.        end;

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: Type "TMyType" is not completly defined
« Reply #11 on: December 08, 2019, 05:24:15 am »
Delphi mode
Code: Pascal  [Select][+][-]
  1.   TMultiSet<T> = record
  2.          fField: byte;
  3.          value: ^TMultiSet<T>;
  4.        end;
Thank you for that.  Would you happen to know how the same thing can be done in regular "FPC mode" ?
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

avk

  • Hero Member
  • *****
  • Posts: 752
Re: Type "TMyType" is not completly defined
« Reply #12 on: December 08, 2019, 06:08:23 am »
Code: Pascal  [Select][+][-]
  1.   generic TMultiSet<T> = record
  2.     fField: byte;
  3.     value: ^TMultiset;
  4.   end;  
  5.  

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Type "TMyType" is not completly defined
« Reply #13 on: December 08, 2019, 11:27:41 am »
I tried your example mate, but sadly I get also here a type error:
(attachment!)

And it actually is correct, since he cant know what <T> should mean without  the generic keyword and/or the brackets in PMultiSet<T> i would assume..

but did you see also the attachment above: it says:

"Error: Internal error: 20010001" or the like
The internal error should not be happening, that should probably be considered a bug in FPC but, that is a determination better left to the developers.

An internal error is always considered a bug, because it should not happen.

Code: Pascal  [Select][+][-]
  1.   generic TMultiSet<T> = record
  2.     fField: byte;
  3.     value: ^TMultiset;
  4.   end;  
  5.  

Or more explicit:

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. {$modeswitch advancedrecords}
  3.  
  4. type
  5.   generic TMultiSet<T> = record
  6.   public type
  7.     PMultiSet = ^specialize TMultiSet<T>;
  8.   public
  9.     fField: Byte;
  10.     value: PMultiSet;
  11.   end;
  12.  

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: Type "TMyType" is not completly defined
« Reply #14 on: December 08, 2019, 02:02:48 pm »

Code: Pascal  [Select][+][-]
  1.   generic TMultiSet<T> = record
  2.     fField: byte;
  3.     value: ^TMultiset;
  4.   end;  
  5.  
Thank you Avk.  That's what I was looking for but for some reason couldn't get there.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018