* * *

Author Topic: NewPascal plans, Generics.Collections and FPC trunk  (Read 15859 times)

Mr.Madguy

  • Sr. Member
  • ****
  • Posts: 364
Re: NewPascal plans, Generics.Collections and FPC trunk
« Reply #120 on: May 15, 2018, 12:13:20 pm »
Yeah, unfortunately this doesn't work. Lazarus complains about no method to override and Delphi is even smarter - it gives "Class type required", when declaring TSubclass. Looks like this fact, that you can do it in Lazarus - is simple bug, that allows undocumented behavior.
Code: Pascal  [Select]
  1.   TAbstractClass = class
  2.     procedure DoSomething;virtual;abstract;
  3.   end;
  4.  
  5.   TImplementationClass = class(TAbstractClass)
  6.     procedure DoSomethingElse;
  7.   end;
  8.  
  9.   TSubclass<T:TAbstractClass> = class(T)
  10.     procedure DoSomething;override;
  11.   end;
  12.  
« Last Edit: May 15, 2018, 12:15:06 pm by Mr.Madguy »
DynamicData 2.0 Released! Many optimizations: 15% smaller sources, 10% smaller code! Improved scalability!
Developing version 2.1.
Crazy unit testing

soerensen3

  • Full Member
  • ***
  • Posts: 152
Re: NewPascal plans, Generics.Collections and FPC trunk
« Reply #121 on: May 15, 2018, 02:09:39 pm »
Quote
Looks like this fact, that you can do it in Lazarus - is simple bug, that allows undocumented behavior.
This is also my guess.
Lazarus 1.9 with FPC 3.0.4
Target: Manjaro Linux 64 Bit (4.9.68-1-MANJARO)

Thaddy

  • Hero Member
  • *****
  • Posts: 6880
Re: NewPascal plans, Generics.Collections and FPC trunk
« Reply #122 on: May 15, 2018, 02:38:17 pm »
Code: Pascal  [Select]
  1.   TAbstractClass = class
  2.     procedure DoSomething;virtual;abstract;
  3.   end;
  4.  
  5.   TImplementationClass = class(TAbstractClass)
  6.     procedure DoSomethingElse;
  7.   end;
  8.  
  9.   TSubclass<T:TAbstractClass> = class(T) // Huh??? What is T (2) doing there? Any clue? Integer? record? single? maybe even an object or do you mean a class.....silly?
  10.     procedure DoSomething;override;
  11.   end;
  12.  
That code is bollocks in any language. Bad code is bad code. Bad programmers are bad programmers. And even proper programmers can write bad code (you just did:QED).
A thoroughly good example, btw, of such nonsense. It is a recursion and can not be resolved to be precise (not even in C++ syntax).

Start with
Code: Pascal  [Select]
  1. TAbstractClass = class<T:class, constructor>;
Which is also nonsense (Of course a class is a class...), but better nonsense.... O:-) 8-)
« Last Edit: May 15, 2018, 03:01:12 pm by Thaddy »
Ada's daddy wrote this:"Fools are my theme, let satire be my song."

soerensen3

  • Full Member
  • ***
  • Posts: 152
Re: NewPascal plans, Generics.Collections and FPC trunk
« Reply #123 on: May 15, 2018, 02:57:48 pm »
Code: Pascal  [Select]
  1.   TAbstractClass = class
  2.     procedure DoSomething;virtual;abstract;
  3.   end;
  4.  
  5.   TImplementationClass = class(TAbstractClass)
  6.     procedure DoSomethingElse;
  7.   end;
  8.  
  9.   TSubclass<T:TAbstractClass> = class(T) // Huh??? What is T (2) doing there? Any clue?
  10.     procedure DoSomething;override;
  11.   end;
  12.  
That code is bollocks in any language. Bad code is bad code. Bad programmers are bad programmers.
A thoroughly good example, btw, of such nonsense. It is a recursion and can not be resolved to be precise (not even in C++).

Start with
Code: Pascal  [Select]
  1. TAbstractClass = class<T:class, constructor>;
Which is also nonsense (Of course a class is a class...), but better nonsense....

I don't understand. Do you mean:
Code: Pascal  [Select]
  1.  
  2. TAbstractClass <T:class, constructor> = class
  3. end;
Because this compiles.
The T in Line 9 means the generic is supposed to inherit T which gets resolved when specializing.
Lazarus 1.9 with FPC 3.0.4
Target: Manjaro Linux 64 Bit (4.9.68-1-MANJARO)

Thaddy

  • Hero Member
  • *****
  • Posts: 6880
Re: NewPascal plans, Generics.Collections and FPC trunk
« Reply #124 on: May 15, 2018, 03:02:40 pm »
That compiles and is correct. For T to resolve during inheritance it needs to be in the <> brackets. Anything else is bollocks.
Note I do not use silly mode. I use {$mode delphi}
« Last Edit: May 15, 2018, 03:04:40 pm by Thaddy »
Ada's daddy wrote this:"Fools are my theme, let satire be my song."

sam707

  • Guest
Re: NewPascal plans, Generics.Collections and FPC trunk
« Reply #125 on: May 15, 2018, 03:02:50 pm »
who seen me as "offensive" because I compare structured mindS and lazy twirled oneS presumed born on a trash with rocket science?

the 1) question is : did you ever meet an offensive person? LOL!!! my answer is NO you r made from sugar and afraid of water

advices :

If some feel concerned, just go and learn (as I suggested => googole is yer friend) hahaha left handled wrote code

hmm @Thaddy +10 on yer last one (even iif I hate you, dark bro) hehehehe
« Last Edit: May 15, 2018, 03:06:15 pm by sam707 »

Thaddy

  • Hero Member
  • *****
  • Posts: 6880
Re: NewPascal plans, Generics.Collections and FPC trunk
« Reply #126 on: May 15, 2018, 03:04:51 pm »
 8-)
Ada's daddy wrote this:"Fools are my theme, let satire be my song."

Mr.Madguy

  • Sr. Member
  • ****
  • Posts: 364
Re: NewPascal plans, Generics.Collections and FPC trunk
« Reply #127 on: May 15, 2018, 05:55:22 pm »
That code is bollocks in any language. Bad code is bad code. Bad programmers are bad programmers. And even proper programmers can write bad code (you just did:QED).
A thoroughly good example, btw, of such nonsense. It is a recursion and can not be resolved to be precise (not even in C++ syntax).

Start with
Code: Pascal  [Select]
  1. TAbstractClass = class<T:class, constructor>;
Which is also nonsense (Of course a class is a class...), but better nonsense.... O:-) 8-)
Well, that's not my code. soerensen3 has said, that such pattern works and it interested me. I've tired it and found out, that even if it partially works - it's compiler bug, as Delphi doesn't allow this trick.

I guess, if this trick would work, this:
Code: Pascal  [Select]
  1.   TSubclass<TImplementationClass>
  2.  
would resolve into this:
Code: Pascal  [Select]
  1.   TSubclass = class(TImplementationClass)
  2.     procedure DoSomething;override;
  3.   end;
  4.  
« Last Edit: May 15, 2018, 06:03:50 pm by Mr.Madguy »
DynamicData 2.0 Released! Many optimizations: 15% smaller sources, 10% smaller code! Improved scalability!
Developing version 2.1.
Crazy unit testing

soerensen3

  • Full Member
  • ***
  • Posts: 152
Re: NewPascal plans, Generics.Collections and FPC trunk
« Reply #128 on: May 15, 2018, 08:55:19 pm »
I guess, if this trick would work, this:
Code: Pascal  [Select]
  1.   TSubclass<TImplementationClass>
  2.  
would resolve into this:
Code: Pascal  [Select]
  1.   TSubclass = class(TImplementationClass)
  2.     procedure DoSomething;override;
  3.   end;
  4.  

Exactly! And this trick DOES work, without any diamonds. Try this code:
Code: Pascal  [Select]
  1. program Project1;
  2. {$MODE DELPHI}
  3.  
  4. uses
  5.   Classes;
  6.  
  7. type
  8.  
  9.   { TAbstractClass }
  10.  
  11.   TAbstractClass < T: class> = class ( T )
  12.     procedure ShowInheritance;
  13.   end;
  14.  
  15. { TAbstractClass }
  16.  
  17. procedure TAbstractClass<T>.ShowInheritance;
  18. var
  19.   C: TClass;
  20. begin
  21.   WriteLn( ClassName + ' inheritance: ' );
  22.   C:= ClassType;
  23.   while ( Assigned( C )) do begin
  24.     WriteLn( '  ' + C.ClassName );
  25.     C:= C.ClassParent;
  26.   end;
  27.   WriteLn( '-----------------' );
  28. end;
  29.  
  30. var
  31.   Obj: TAbstractClass < TPersistent >;
  32.   Obj2: TAbstractClass < TStringList >;
  33.  
  34. begin
  35.   Obj:= ( TAbstractClass < TPersistent >).Create;
  36.   Obj2:= ( TAbstractClass < TStringList >).Create;
  37.   Obj.ShowInheritance;
  38.   Obj2.ShowInheritance;
  39.   Obj.Free;
  40.   Obj2.Free;
  41. end.
  42.  

This is the output:
Code: [Select]
TAbstractClass<Classes.TPersistent> inheritance:
  TAbstractClass<Classes.TPersistent>
  TPersistent
  TObject
-----------------
TAbstractClass<Classes.TStringList> inheritance:
  TAbstractClass<Classes.TStringList>
  TStringList
  TStrings
  TPersistent
  TObject
-----------------

I also said it is probably a bug and I also made a bugreport about it some time ago. It is still open (https://bugs.freepascal.org/view.php?id=32054).

Code: Pascal  [Select]
  1. TAbstractClass = class<T:class, constructor>;
This also makes no sense in any syntax. Apart from the missing brackets for the inheritance part the < T > part has to be on the left side. After that the type T is declared and can be used without the <> (Except for the function name in the body in Delphi mode).
This is from the RADStudio documentation (http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Declaring_Generics). I added some comments.

Code: Pascal  [Select]
  1.  type
  2.    TPair<TKey,TValue> { TKey and TValue are declared here and can be used without <> from here on} = class   // TKey and TValue are type parameters
  3.      FKey: TKey; // If type TKey can be used here, why not in the inheritance part after the class keyword?
  4.      FValue: TValue;
  5.      function GetValue: TValue;
  6.    end;
Embarcadero just decided not to support this behaviour. This doesn't mean it makes no sense. They could have decided for the opposite as well. Whoever wrote this part of the fpc compiler propbably did not think of this option, so it does not generate an error.
Lazarus 1.9 with FPC 3.0.4
Target: Manjaro Linux 64 Bit (4.9.68-1-MANJARO)

Mr.Madguy

  • Sr. Member
  • ****
  • Posts: 364
Re: NewPascal plans, Generics.Collections and FPC trunk
« Reply #129 on: May 16, 2018, 06:19:14 am »
This pattern is useless for me, if it can't override virtual methods. What is also bad about it: as this solution is based on using generics, it allows me to reuse code at source level only - at binary level code is still duplicated. And one of things, I actually want to avoid - is binary size inflation. That's why I stopped using the simplest possible solution, I've used in version 0.x - full reimplementation of every abstract class. And what is good about multiple inheritance - it allows binary code reusing.
DynamicData 2.0 Released! Many optimizations: 15% smaller sources, 10% smaller code! Improved scalability!
Developing version 2.1.
Crazy unit testing

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus