Lazarus

Free Pascal => General => Topic started by: Warfley on July 11, 2020, 07:03:41 pm

Title: Generic function returning pointer
Post by: Warfley on July 11, 2020, 07:03:41 pm
Hey, I was writing a simple generic function that should return a pointer to it's generic parameter type:
Code: Pascal  [Select][+][-]
  1. generic function foo<T>(): ^T;

The usual way of defining the pointer as it's own type is of course not possible in this situation. Is there another way except using untyped pointers?

I've already tried out defining a generic pointer type
Code: Pascal  [Select][+][-]
  1.   generic TPointer<T> = ^T;
  2.  
  3.   TFoo = class
  4.     generic function Foo<T>(): TPointer<T>;
  5.   end;    
But this seems to also be pretty broken because this gives me my favourite error regarding generics, internal error 2012101001:
Quote
project1.lpr(16,3) Error: This type cannot be a generic
project1.lpr(17,54) Error: Internal error 2012101001

Mode Delphi: the same effect
Note line 16 is TFoo = class line
Title: Re: Generic function returning pointer
Post by: bytebites on July 11, 2020, 07:34:43 pm
Code: Pascal  [Select][+][-]
  1.   generic TFoo<T> = class
  2.     type
  3.     TPointer = ^T;
  4.     function Foo(): TPointer;
  5.   end;  
Title: Re: Generic function returning pointer
Post by: Warfley on July 11, 2020, 08:37:49 pm
Not possible in my specific case, because this is already a class method to a class which can not be generic
Title: Re: Generic function returning pointer
Post by: jamie on July 11, 2020, 10:50:34 pm
the generic parameter being the type that is requested, shouldn't you be using TCLASS for reference to a class body ?

 I don't know but looking at the error message I would say someone was up early in the morning when they applied that error code.
Title: Re: Generic function returning pointer
Post by: PascalDragon on July 12, 2020, 12:17:02 pm
Hey, I was writing a simple generic function that should return a pointer to it's generic parameter type:
Code: Pascal  [Select][+][-]
  1. generic function foo<T>(): ^T;

The usual way of defining the pointer as it's own type is of course not possible in this situation. Is there another way except using untyped pointers?

At least with only generic routines there isn't. And no, Delphi doesn't allow that either.

I've already tried out defining a generic pointer type
Code: Pascal  [Select][+][-]
  1.   generic TPointer<T> = ^T;
  2.  
  3.   TFoo = class
  4.     generic function Foo<T>(): TPointer<T>;
  5.   end;    
But this seems to also be pretty broken because this gives me my favourite error regarding generics, internal error 2012101001:
Quote
project1.lpr(16,3) Error: This type cannot be a generic
project1.lpr(17,54) Error: Internal error 2012101001

Two errors on your side: generics of pointers are not supported (so that should already error out) and since you're using mode ObjFPC you must use specialize TPointer<T>. Though of course the compiler shouldn't complain with an internal error anyway... would you please report that?

The following (admitably clumsy workaround) would work however:

Code: Pascal  [Select][+][-]
  1. program tgentest3;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$modeswitch advancedrecords}
  5.  
  6. type
  7.   generic TPointerType<T> = record
  8.   public type
  9.     PT = ^T;
  10.   end;
  11.  
  12. generic function Test<T>: specialize TPointerType<T>.PT;
  13. begin
  14. end;
  15.  
  16. begin
  17. end.
Title: Re: Generic function returning pointer
Post by: jamie on July 12, 2020, 12:36:39 pm
Better to error over silently generating false hope.
Title: Re: Generic function returning pointer
Post by: PascalDragon on July 12, 2020, 04:57:12 pm
I don't know but looking at the error message I would say someone was up early in the morning when they applied that error code.

Internal errors follow the format YYYYMMDDXX where XX is a unique number (usually ascending from 01). And they don't have a specific message, because they not supposed to be triggered and thus point to a bug in the compiler.
TinyPortal © 2005-2018