Recent

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

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12156
  • FPC developer.
Re: NewPascal plans, Generics.Collections and FPC trunk
« Reply #75 on: May 14, 2018, 10:39:11 am »
2) Sorry, but this is the reason, why some people think, that C++ is better, than Pascal, and that at some point, when you become professional enough - you should migrate to C++. I've analysed it many times - there are no other way to complete my task in Pascal, other than so called class composition - i.e. storing references to ancestor classes right inside descendant classes and declaring 3-4 levels of wrapper methods.

It is a very rare pattern, and usually there are other ways around it (in the class composition) to work around it. One could argue needing it is a code smell.

There are maybe a few cases left, specially on the lowest level in runtime libraries

Specially for such lowlevel simple cases, in theory devirtualization and other global optimization techniques might reduce overheads of workarounds by inlining the delegated interface methods in the main class, eliminating the reference


Mr.Madguy

  • Hero Member
  • *****
  • Posts: 869
Re: NewPascal plans, Generics.Collections and FPC trunk
« Reply #76 on: May 14, 2018, 11:16:15 am »
It is a very rare pattern, and usually there are other ways around it (in the class composition) to work around it. One could argue needing it is a code smell.

There are maybe a few cases left, specially on the lowest level in runtime libraries

Specially for such lowlevel simple cases, in theory devirtualization and other global optimization techniques might reduce overheads of workarounds by inlining the delegated interface methods in the main class, eliminating the reference
I don't think, that container pattern is rare.
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

Thaddy

  • Hero Member
  • *****
  • Posts: 16811
  • Ceterum censeo Trump esse delendam
Re: NewPascal plans, Generics.Collections and FPC trunk
« Reply #77 on: May 14, 2018, 11:23:44 am »
friends are not container patterns - the opposite is true - and are easily implemented with interfaces.
It should also be possible with a pure abstract and sealed pascal intermediate advanced record and a type cast, btw. That's how I initially got Steinberg's ASIO working on FPC and Delphi:
Make sure first VMT entry you are interested in start at offset 0. The FPC default entries have negative offsets, just like Delphi.

BTW it is enlightening to google for Bjarne Soustrup and "friends" in C++......(While E. Coyote)
« Last Edit: May 14, 2018, 11:35:41 am by Thaddy »
Changing servers. thaddy.com may be temporary unreachable but restored when the domain name transfer is done.

hnb

  • Sr. Member
  • ****
  • Posts: 270
Re: NewPascal plans, Generics.Collections and FPC trunk
« Reply #78 on: May 14, 2018, 11:35:29 am »
Make sure first VMT entry you are interested in start at offset 0. The FPC default entries have negative offsets, just like Delphi.

probably this is not true anymore, from RTL :
Code: Pascal  [Select][+][-]
  1.        { These were negative value's, but are now positive, else classes
  2.          couldn't be used with shared linking which copies only all data from
  3.          the .global directive and not the data before the directive (PFV) }  
« Last Edit: May 14, 2018, 11:42:59 am by hnb »
Checkout NewPascal initiative and donate beer - ready to use tuned FPC compiler + Lazarus for mORMot project

best regards,
Maciej Izak

Thaddy

  • Hero Member
  • *****
  • Posts: 16811
  • Ceterum censeo Trump esse delendam
Re: NewPascal plans, Generics.Collections and FPC trunk
« Reply #79 on: May 14, 2018, 11:38:32 am »
Maciej, as far as I know it is when the record contains only abstract methods and is sealed too, the typecast on an external reference will work.
I will test, though. Would be bad if it does not work anymore, because it was a way to avoid flattening C++ classes....And these assume a VMT starting at 0.

Anyway friend patterns are not container patterns and easily solved with interfaces.
« Last Edit: May 14, 2018, 11:42:27 am by Thaddy »
Changing servers. thaddy.com may be temporary unreachable but restored when the domain name transfer is done.

hnb

  • Sr. Member
  • ****
  • Posts: 270
Re: NewPascal plans, Generics.Collections and FPC trunk
« Reply #80 on: May 14, 2018, 12:14:13 pm »
3) Assignment operator isn't supported, as I know. You can't declare "operator Implicit(A:TMyType):TMyType" - compiler would complain about it.

with NewPascal you can use management operators: AddRef and Copy instead of "operator Implicit(A:TMyType):TMyType", see:

https://github.com/newpascal/freepascal/blob/330aad68b600d7748fd337afa9aad93a4ee35464/tests/test/tmoperator8.pp
Checkout NewPascal initiative and donate beer - ready to use tuned FPC compiler + Lazarus for mORMot project

best regards,
Maciej Izak

Thaddy

  • Hero Member
  • *****
  • Posts: 16811
  • Ceterum censeo Trump esse delendam
Re: NewPascal plans, Generics.Collections and FPC trunk
« Reply #81 on: May 14, 2018, 12:20:44 pm »
Indeed, and that was a very valuable contribution. But it is also in trunk.
Changing servers. thaddy.com may be temporary unreachable but restored when the domain name transfer is done.

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 869
Re: NewPascal plans, Generics.Collections and FPC trunk
« Reply #82 on: May 14, 2018, 12:24:17 pm »
friends are not container patterns - the opposite is true - and are easily implemented with interfaces.
It should also be possible with a pure abstract and sealed pascal intermediate advanced record and a type cast, btw. That's how I initially got Steinberg's ASIO working on FPC and Delphi:
Make sure first VMT entry you are interested in start at offset 0. The FPC entries have negative offsets, just like Delphi.
Yeah, I know, that it's possible to be implemented via interfaces and 2nd level of abstraction in my project actually uses them. And I know, that I can use some crazy methods, like constructing classes/interfaces at runtime. I can also generate binary code at runtime, yeah. But all this solutions are clunky, don't you think? I just want to write clear and consistent code, like this:

Code: C++  [Select][+][-]
  1. template <class TKey, class TValue>
  2. struct TPair {
  3.   TKey Key;
  4.   TValue Value;
  5. };
  6.  
  7. template <class T>
  8. class TAbstractList {
  9. };
  10.  
  11. //Implementation
  12.  
  13. template <class T>
  14. class TList:virtual TAbstractList {
  15. };
  16.  
  17. //Pair list - reuses List features, but adds some pair-specific ones
  18.  
  19. template <class TKey, class TValue>
  20. class TAbstractPairList:virtual TAbstractList<TPair<TKey, TValue>> {
  21. };
  22.  
  23. //Please note! Implementation REUSES TList code
  24.  
  25. template <class TKey, class TValue>
  26. class TPair:virtual TList<TPair<TKey, TValue>>, virtual TAbstractPairList<TKey, TValue> {
  27. };
  28.  
  29. template <class T>
  30. class TAbstractSet {
  31. };
  32.  
  33. //Implementation - REUSES TList code!
  34.  
  35. template <class T>
  36. class TSet:virtual TList<T>, virtual TAbstractSet<T> {
  37. };
  38.  
  39. template <class TKey, class TValue>
  40. class TAbstractDictionary:virtual TAbstractSet<TPair<TKey, TValue>> {
  41. };
  42.  
  43. //Implementation - REUSES TPairList, TSet and TList!
  44.  
  45. template <class TKey, class TValue>
  46. class TDictionary:virtual TPairList<TKey, TValue>, virtual TSet<TPair<TKey, TValue>>, virtual TAbstractDictionary<TKey, TValue> {
  47. }
  48.  
And slight modification would allow us to cast set to list and dictionary to any other class.
Code: C++  [Select][+][-]
  1. template <class T>
  2. class TAbstractSet:virtual TAbstractList<T> {
  3. };
  4.  
  5. template <class TKey, class TValue>
  6. class TAbstractDictionary:virtual TAbstractPairList<TKey, TValue>, virtual TAbstractSet<TPair<TKey, TValue>> {
  7. };
  8.  
« Last Edit: May 14, 2018, 12:53:34 pm by Mr.Madguy »
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

hnb

  • Sr. Member
  • ****
  • Posts: 270
Re: NewPascal plans, Generics.Collections and FPC trunk
« Reply #83 on: May 14, 2018, 12:40:16 pm »
Indeed, and that was a very valuable contribution. But it is also in trunk.
yes it is in trunk but for how long? The future of this feature is not safe in FPC trunk (I guess it can be removed). This is because:

* without FastRTTI it has some side effects
* Management Operators are more valuable with default field/property feature for smart pointers. Without this feature MO operators are not much usable
* my work for default field/properties is totally blocked in trunk, I am banned in core so it can't be discussed in normal way
* Management operators are in progress. There is idea for two more operators for safe/fast work with threads

without FastRTTI, without extensions and without "default field" feature, MO functionality should be removed from trunk because it brings more negatives than gains.
Checkout NewPascal initiative and donate beer - ready to use tuned FPC compiler + Lazarus for mORMot project

best regards,
Maciej Izak

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 869
Re: NewPascal plans, Generics.Collections and FPC trunk
« Reply #84 on: May 14, 2018, 01:00:04 pm »
with NewPascal you can use management operators: AddRef and Copy instead of "operator Implicit(A:TMyType):TMyType", see:

https://github.com/newpascal/freepascal/blob/330aad68b600d7748fd337afa9aad93a4ee35464/tests/test/tmoperator8.pp
Yeah, that looks like what I actually want. But how about automated try...finally...end feature, interfaces have?
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

hnb

  • Sr. Member
  • ****
  • Posts: 270
Re: NewPascal plans, Generics.Collections and FPC trunk
« Reply #85 on: May 14, 2018, 01:02:42 pm »
Yeah, that looks like what I actually want. But how about automated try...finally...end feature, interfaces have?
Finalize operator is called even when exception occurs, so you don't need to use "try finally end" for records with Management Operators.
Checkout NewPascal initiative and donate beer - ready to use tuned FPC compiler + Lazarus for mORMot project

best regards,
Maciej Izak

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 869
Re: NewPascal plans, Generics.Collections and FPC trunk
« Reply #86 on: May 14, 2018, 01:14:25 pm »
Finalize operator is called even when exception occurs, so you don't need to use "try finally end" for records with Management Operators.
Ok. Only multiple inheritance left and I'll be happy.
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

Thaddy

  • Hero Member
  • *****
  • Posts: 16811
  • Ceterum censeo Trump esse delendam
Re: NewPascal plans, Generics.Collections and FPC trunk
« Reply #87 on: May 14, 2018, 03:57:10 pm »
Multiple inheritance would make me freeze the last build before it was introduced and never look forward again.
It is the single baddest thing to - out of many - have, as Bjarne Soustrup agrees, in C++.
And we have multiple inheritance through interfaces, which is less likely to cause stupid non-maintainable code.

Look at it in the light of "with" syntax: it causes mayhem, because people start to use it...Never trust programmers...
They tend to forget or not know about scope...
Multiple inheritance - C++ style - is BAD!. Just like "with" is. Multiple inheritance interface style is sane, but we have that already.

There is no sane way to resolve name clashes using C++ other than scoping explicitly. Which renders it useless. If I see it during code reviews, I insist on having that code changed/removed.
[edit]
Note Maciej's management operators (and Sven, he made some contributions if I am not mistaken) work very, very well and I use them a lot in new code.
« Last Edit: May 14, 2018, 04:07:20 pm by Thaddy »
Changing servers. thaddy.com may be temporary unreachable but restored when the domain name transfer is done.

soerensen3

  • Full Member
  • ***
  • Posts: 213
Re: NewPascal plans, Generics.Collections and FPC trunk
« Reply #88 on: May 14, 2018, 04:29:44 pm »
How about allowing this for generics:

Code: Pascal  [Select][+][-]
  1.   TMakeSpatial < T: TObject > = class ( T )
  2.       //[..]
  3.       procedure RotateAroundAxis( Axis: TVec3; Angle: Single );
  4.       property Position: TVec3 read FPosition write FPosition;
  5.       property Rotation: TVec3 read FRotation write FRotation;
  6.   end;
  7.  
  8.   TMakeRenderable < T: TObject > = class ( T )
  9.       //[..]
  10.       procedure Render;
  11.       property Visible: Boolean read FVisible write FVisible;
  12.   end;
  13.  
  14.  
  15.   TSomeClass = class( TMakeSpatial < TObject > )
  16.   end;
  17.  
  18.   TAnotherClass = class( TMakeSpatial < TMakeRenderable < TObject > > )
  19.   end;
  20.  
  21.   TThirdClass = class( TMakeRenderable< TObject >);
  22.  

I think it's perfectly understandable, doesn't require many changes in the compiler (I think so because it is even possible but does not work in some cases). It does not require real multiple inheritance but it has all the benefits of it. Even "is" and "as" operators work in combination with interfaces. I've used this for some time in my engine and removed it because it did not work in all cases and code completion does not support it. I think it is more a hack than a feature at the moment.
Lazarus 1.9 with FPC 3.0.4
Target: Manjaro Linux 64 Bit (4.9.68-1-MANJARO)

hnb

  • Sr. Member
  • ****
  • Posts: 270
Re: NewPascal plans, Generics.Collections and FPC trunk
« Reply #89 on: May 14, 2018, 04:30:00 pm »
Note Maciej's management operators (and Sven, he made some contributions if I am not mistaken) work very, very well and I use them a lot in new code.
It was consulted with Jonas, Florian and Sven. Management Operators functionality was made by me. IIRC when final patch was ready, Sven was against the management operators and he did not want them in trunk.
Checkout NewPascal initiative and donate beer - ready to use tuned FPC compiler + Lazarus for mORMot project

best regards,
Maciej Izak

 

TinyPortal © 2005-2018