Recent

Author Topic: [SOLVED] Nested Functions and Generics  (Read 2470 times)

MountainQ

  • Jr. Member
  • **
  • Posts: 65
[SOLVED] Nested Functions and Generics
« on: April 26, 2018, 09:30:52 am »
Hello everybody,

concerning the case when combining generics and nested functions:
I have a generic object; and I would like to have a nested function within one its procedures.
This is pretty clearly denied:
Quote
Generic methods cannot have nested procedures
Is there a good reason for this?
Does anyone know a workaround?
Any chance this might change in the future?
I could not find anything.
Thanks

« Last Edit: April 27, 2018, 01:57:23 pm by MountainQ »

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Nested Functions and Generics
« Reply #1 on: April 26, 2018, 11:21:51 am »
Well, you can not do this:
Code: Pascal  [Select][+][-]
  1. type
  2.   TMyclass<T>
  3.   public
  4.     procedure MyNestedProcedure<T>; // that's a double declaration of T!
  5.   end;
But you can do this:
Code: Pascal  [Select][+][-]
  1. type
  2.   TMyclass<T> = class
  3.   public
  4.     procedure MyNestedProcedure(const a:T);
  5.   end;
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

MountainQ

  • Jr. Member
  • **
  • Posts: 65
Re: Nested Functions and Generics
« Reply #2 on: April 26, 2018, 11:37:50 am »
Thank you Thaddy for your quick reply.
I hope I did not misstate my problem.
What I would like to have is something like: (I use the FreePascal style of generics because of the editor)

Code: Pascal  [Select][+][-]
  1. type
  2.   generic TRedBlackTree<_el> = class
  3.   public
  4.     function print: string;
  5.   end;
  6.  
  7. ...
  8.  
  9. function TRedBlackTree.print: string;
  10.   function DoSomething(): string;
  11.   begin
  12.   end;
  13. begin
  14.   traversetree(@DoSomething);
  15. end;
  16.  
It is the declaration of the function DoSomething that raises the error.
« Last Edit: April 26, 2018, 04:11:49 pm by MountainQ »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: Nested Functions and Generics
« Reply #3 on: April 27, 2018, 12:08:28 pm »
What I would like to have is something like: (I use the FreePascal style of generics because of the editor)

It is the declaration of the function DoSomething that raises the error.

That restriction was lifted in trunk (3.1.1) though if I remember correctly Delphi disallows it as well...

MountainQ

  • Jr. Member
  • **
  • Posts: 65
Re: Nested Functions and Generics
« Reply #4 on: April 27, 2018, 01:57:03 pm »
Thank you PascalDragon; in particular for helping develop FreePascal.
I am using FPC 3.0.4.; so I have to be a little patient.

 

TinyPortal © 2005-2018