Recent

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

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Type "TMyType" is not completly defined
« Reply #30 on: December 13, 2019, 09:44:37 am »
Ok first, thx for the answers, brought me much further!

second:
   * I have to ask, why is it not allowed to have only 1 definition of "PNode = ^specialize TNode<T>" in a global type
      section, like within an "interface"-section? why must I have it written in each record/class -type  section?
      I understand partly, that the "<T>" is needed somehow to say its a pointer of a TNode<T> and the<T>is not
      known otherwise, but cant you guys make it possible, to write only this and still make the compiler understand
      that: <"declare a pointer to a TNode, even without now knowing what "T" is, so whatever the specialization of
      "T" will be, I just want to have a pointer type of that type">

      I hope I could my suggestion/question formulate properly and understandably ^^

   
It is not possible to have a general PNode as the compiler needs to know the size of the right side so that Inc or array access work correctly.

What would technically be possible would be what you had suggested earlier with generic PNode<T> = ^specialize TNode<T>, but it would not safe anything:
Code: Pascal  [Select][+][-]
  1. type
  2.   generic TNode<T> = record
  3.   end;
  4.  
  5.   generic PNode<T> = ^specialize TNode<T>;
  6.  
  7.   generic TSomething<T> = record
  8.     function CreateNode: specialize PNode<T>;
  9.   end;
You see the declaration of CreateNode? To avoid having to type that out I'd have to do this anyway:
Code: Pascal  [Select][+][-]
  1.   generic TSomething<T> = record
  2.   public type
  3.     MyPNode = specialize PNode<T>;
  4.   public
  5.     function CreateNode: MyPNode;
  6.   end;

So there is no real difference between the existing solution and your proposed on except one single ^.

Also records can only be forward declared for the use in pointers (which would be the case here), but for generics that provides a whole new box of potential problems. (There is a bug report to allow forward declarations of generic classes for Delphi compatibility, but that needs to be handled with care as well)

Shpend

  • Full Member
  • ***
  • Posts: 167
Re: Type "TMyType" is not completly defined
« Reply #31 on: December 13, 2019, 01:53:35 pm »
OK I see the issue :D

But nice to get a better understanding of how generics work in FPC!



Shpend

  • Full Member
  • ***
  • Posts: 167
Re: Type "TMyType" is not completly defined
« Reply #32 on: December 13, 2019, 02:03:11 pm »
ah and also:

why does this code still produces a: "compilation aborted" error?


Code: Pascal  [Select][+][-]
  1.   procedure DoStuff;
  2.      type
  3.        TTestArray = specialize TDoublyLinkedArray<byte>;
  4.       var
  5.         list: TTestArray;
  6.    begin
  7.       list := TTestArray.Create(0, false);
  8.    end;  
  9.  

Sidenotes:
- windows 10 pro
- CPU=x64
- FPC 3.0.4 (current stable version) 64bit compiler
- lazarus IDE: 2.0.6
« Last Edit: December 13, 2019, 02:15:21 pm by Shpend »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Type "TMyType" is not completly defined
« Reply #33 on: December 14, 2019, 10:50:31 am »
Would you please provide a simple, complete, compilable example (e.g. I don't know what TDoublyLinkedArray is declared as) as well as the full output you receive? (If you use Lazarus you need to right click in the message window where there is a menu item to copy all messages)

Shpend

  • Full Member
  • ***
  • Posts: 167
Re: Type "TMyType" is not completly defined
« Reply #34 on: December 14, 2019, 04:56:30 pm »
Hi, @PascalDragon

I have DM you an attachment, it says: the abortion of the compilation process has to do something with the an AccessViolation, but it only occurs when I make use of:

Code: Pascal  [Select][+][-]
  1.  
  2. type
  3.   TList = specialize TDoublyLinkedArray<byte>;   //or the same shit happens when i do:
  4.  
  5. var
  6.    list1: specialize TDoublyLinkedArray<byte>      {just without the TList in this case but also with, see:}
  7.    list2: TList;  // same error
  8.  

Shpend

  • Full Member
  • ***
  • Posts: 167
Re: Type "TMyType" is not completly defined
« Reply #35 on: December 14, 2019, 05:00:05 pm »
see the testcase.zip for this issue, all files needed are there!

avk

  • Hero Member
  • *****
  • Posts: 752
Re: Type "TMyType" is not completly defined
« Reply #36 on: December 14, 2019, 07:28:54 pm »
What happens if you replace in your code
Code: Pascal  [Select][+][-]
  1.       generic TElement<T> = record
  2.         DeletedAt, SwapedAt: TIndex;
  3.         Origin: record
  4.           Value: T;
  5.           Index: TIndex;
  6.         end;
  7.  
  8.         //class operator = (const this, other: TElement) : Boolean;
  9.         //class operator <>(const this, other: TElement) : Boolean;
  10.  
  11.         case isArraySorted: boolean of
  12.           True:  (InsertedBetween: array[0..1] of TIndex;);
  13.           False: (InsertedAt: TIndex);
  14.       end;  
  15.  
with
Code: Pascal  [Select][+][-]
  1.   generic TElement<T> = record
  2.   type
  3.     TOrigin = record
  4.       Value: T;
  5.       Index: TIndex;
  6.     end;
  7.   var
  8.     DeletedAt, SwapedAt: TIndex;
  9.     Origin: TOrigin;
  10.     case isArraySorted: boolean of
  11.       True:  (InsertedBetween: array[0..1] of TIndex;);
  12.       False: (InsertedAt: TIndex);
  13.   end;  
  14.  

Shpend

  • Full Member
  • ***
  • Posts: 167
Re: Type "TMyType" is not completly defined
« Reply #37 on: December 14, 2019, 08:00:04 pm »
What happens if you replace in your code
Code: Pascal  [Select][+][-]
  1.       generic TElement<T> = record
  2.         DeletedAt, SwapedAt: TIndex;
  3.         Origin: record
  4.           Value: T;
  5.           Index: TIndex;
  6.         end;
  7.  
  8.         //class operator = (const this, other: TElement) : Boolean;
  9.         //class operator <>(const this, other: TElement) : Boolean;
  10.  
  11.         case isArraySorted: boolean of
  12.           True:  (InsertedBetween: array[0..1] of TIndex;);
  13.           False: (InsertedAt: TIndex);
  14.       end;  
  15.  
with
Code: Pascal  [Select][+][-]
  1.   generic TElement<T> = record
  2.   type
  3.     TOrigin = record
  4.       Value: T;
  5.       Index: TIndex;
  6.     end;
  7.   var
  8.     DeletedAt, SwapedAt: TIndex;
  9.     Origin: TOrigin;
  10.     case isArraySorted: boolean of
  11.       True:  (InsertedBetween: array[0..1] of TIndex;);
  12.       False: (InsertedAt: TIndex);
  13.   end;  
  14.  
Good news:
  It compiles fine! (but why does the problem seemingly rely on the origin:record... stuff, it should also have
  worked as such, shouldnt it?

Bad news: 
  It compiles, however: when i want to edit the "create" function of the TDoublyLinkedArraay<T> who again makes use of the TNode<T> who again makes use of ur edited TElement<T> record, I cant press [CTRL] + MouseClick like jumping through the editor to reach another line of code, because it tells me for some reason:

"Case only allowed in record"?

I am confused :D
 

Shpend

  • Full Member
  • ***
  • Posts: 167
Re: Type "TMyType" is not completly defined
« Reply #38 on: December 14, 2019, 08:05:19 pm »
look attachment:

(When i want to press [Ctrl] + ENTER to get syntax-completion it jumps to the red line in the image attached and says: "Case only allowed in records"

I have btw a concern, even when this works, like:

Am i able to assign a value of PNode which comes from TNode<T> type-section to a PNode-variabletype which comes from TDoublyLinkedArray<T> type-section, hence my issue, that i wanted a globally PNode, since i am afraid that it doesnt assign, since it sees both of these as different types, technically, even tho, they are symantically same.
« Last Edit: December 14, 2019, 08:11:03 pm by Shpend »

avk

  • Hero Member
  • *****
  • Posts: 752
Re: Type "TMyType" is not completly defined
« Reply #39 on: December 14, 2019, 08:31:28 pm »
This seems to be a Codetools bug.
However, there is also a compiler bug. Let's wait for what @PascalDragon will say.

Shpend

  • Full Member
  • ***
  • Posts: 167
Re: Type "TMyType" is not completly defined
« Reply #40 on: December 14, 2019, 08:58:08 pm »
This seems to be a Codetools bug.
However, there is also a compiler bug. Let's wait for what @PascalDragon will say.

To the first, yes it must be, since when i dont use the syntax completion, I can still compile the code and it runs perfectly fine.

And to my early "issue" that the assignment from different PNodes to eachother is gone too, since they are assignable to eachother which makes sense, great so far.

Now, where do yozu assume a compiler bug, as I said, the error is gone, because it had not something to do with the specialize TDoublyLinkedArray<T> codeline, or am I wrong?

avk

  • Hero Member
  • *****
  • Posts: 752
Re: Type "TMyType" is not completly defined
« Reply #41 on: December 15, 2019, 06:45:04 am »
Yes, you see that the compiler finds the problem in TElement and at first glance it is not clear why it successfully compiles the second variant but crashes on the first.
And I suppose "Compilation raised exception internally" always means a compiler bug.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Type "TMyType" is not completly defined
« Reply #42 on: December 15, 2019, 06:34:36 pm »
It seems that anonymous records are not yet handled correctly inside generics. Please report it as a bug so that it isn't forgotten, but only include the source files and try to get rid of the Globals.pas (just copy the required types to the doublyLinkedArray.pas).

avk

  • Hero Member
  • *****
  • Posts: 752
Re: Type "TMyType" is not completly defined
« Reply #43 on: December 16, 2019, 07:02:33 am »
Done, 0036446

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Type "TMyType" is not completly defined
« Reply #44 on: December 16, 2019, 09:09:32 am »
Thanks.

 

TinyPortal © 2005-2018