* * *

Author Topic: Can a type helper on a dynamic array create its own instance ?  (Read 617 times)

Nitorami

  • Sr. Member
  • ****
  • Posts: 335
Is it legal to call a type helper to initialise a dynamic array ? In this code, the helper "make" works as intended, although the array has not been set a length yet, and "assigned" returns false. With a Class, that would not be possible. It seems that for dynamic arrays "assigned" is just an alias for length=0, and helper methods can be used anytime.

Code: Pascal  [Select]
  1. {$modeswitch typehelpers}
  2.  
  3. type TMatrix = array of array of real;
  4.  
  5. type TMatHelper = type helper for TMatrix
  6.         procedure Make (dimx,dimy: integer);
  7.      end;
  8.  
  9. procedure TMatHelper.Make (dimx,dimy: integer);
  10. begin
  11.   SetLength (self,dimx,dimy);
  12. end;
  13.  
  14.  
  15. procedure print (M: TMatrix);
  16. var x,y: integer;
  17. begin
  18.   for x := low(M) to high(M) do begin
  19.     for y := low(M[x]) to high(M[x]) do write (m[x,y]:8:3);
  20.     writeln;
  21.   end;
  22. end;
  23.  
  24. var M1: TMatrix;
  25. begin
  26.   writeln ('Assigned: ',Assigned(M1));
  27.   M1.Make (3,3);
  28.   Print (M1);
  29.   writeln ('Assigned: ',Assigned(M1));
  30. end.
  31.  

Output:
Quote
Running "c:\tmp\mist.exe "
Assigned: FALSE
   0.000   0.000   0.000
   0.000   0.000   0.000
   0.000   0.000   0.000
Assigned: TRUE

taazz

  • Hero Member
  • *****
  • Posts: 5153
Re: Can a type helper on a dynamic array create its own instance ?
« Reply #1 on: July 08, 2018, 10:10:24 pm »
as expected yes. take a closer look on how is the setlength declared .
Code: Pascal  [Select]
  1. procedure SetLength(var S: AStringType; Len: SizeInt);
  2. procedure SetLength(var A: DynArrayType; Len: SizeInt);
  3.  
so when you pass an array you pass a known memory address that has a value of nil. That value is replaced with a new array.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

ASerge

  • Hero Member
  • *****
  • Posts: 830
Re: Can a type helper on a dynamic array create its own instance ?
« Reply #2 on: July 08, 2018, 10:39:31 pm »
Is it legal to call a type helper to initialise a dynamic array ?
Why not, if they're even allowed to have constructors: considerations for (simple) type helpers.

VTwin

  • Sr. Member
  • ****
  • Posts: 425
  • Former Turbo Pascal 3 user
Re: Can a type helper on a dynamic array create its own instance ?
« Reply #3 on: July 09, 2018, 12:52:41 am »
Wow, I did not realize this. Very cool. So apparently I can rewrite code:

Code: Pascal  [Select]
  1. a := Det(b);
  2. c := Transpose(b);

by using helpers, with:
 
Code: Pascal  [Select]
  1. a := b.Det;
  2. c := b.Transpose;

I'm not sure how much of an improvement that is, or if I would want to recode, but it presents interesting possibilities for a matrix library. I tend to mimic MATLAB or R functionality when possible. Thanks.
« Last Edit: July 09, 2018, 12:57:35 am by VTwin »
“Talk is cheap. Show me the code.” Linus Torvalds

Lazarus 1.8.4, FPC 3.0.4
macOS 10.11.5 (32 bit Carbon, working on Cocoa) 
Windows 7 (64 bit)
Ubuntu 16.04.3 (64 bit)

VTwin

  • Sr. Member
  • ****
  • Posts: 425
  • Former Turbo Pascal 3 user
Re: Can a type helper on a dynamic array create its own instance ?
« Reply #4 on: July 09, 2018, 01:21:52 am »
As an aside, I have a fairly large (~50,000 line) matrix library that I am trying to figure out how to license, GPL or LGPL. Ideally I'd like to contribute to Lazarus/FPC. The library includes optimized code for 2D and 3D vector graphics, operator overloading, sorting, etc. On top of that is a vector graphics library with bitmap (working with BGRABitmap), svg, eps, dxf output, that includes layers, and many other options. If anyone with numerical or graphics experience has suggestions I'd be interested in a conversation.
« Last Edit: July 09, 2018, 01:52:40 am by VTwin »
“Talk is cheap. Show me the code.” Linus Torvalds

Lazarus 1.8.4, FPC 3.0.4
macOS 10.11.5 (32 bit Carbon, working on Cocoa) 
Windows 7 (64 bit)
Ubuntu 16.04.3 (64 bit)

ASerge

  • Hero Member
  • *****
  • Posts: 830
Re: Can a type helper on a dynamic array create its own instance ?
« Reply #5 on: July 09, 2018, 07:39:26 am »
Wow, I did not realize this. Very cool. So apparently I can rewrite code:
A little upset: Parameterless constructors are not allowed in records or record/type helpers.

avra

  • Hero Member
  • *****
  • Posts: 1298
    • Additional info
Re: Can a type helper on a dynamic array create its own instance ?
« Reply #6 on: July 09, 2018, 11:09:13 am »
I am trying to figure out how to license, GPL or LGPL. Ideally I'd like to contribute to Lazarus/FPC.
This would be ideal for compatibility with FPC/LAZ:
http://wiki.freepascal.org/FPC_modified_LGPL
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

taazz

  • Hero Member
  • *****
  • Posts: 5153
Re: Can a type helper on a dynamic array create its own instance ?
« Reply #7 on: July 09, 2018, 12:15:48 pm »
As an aside, I have a fairly large (~50,000 line) matrix library that I am trying to figure out how to license, GPL or LGPL.
Depends on your goals of releasing the library. There are various licenses to suit any need.

For example you can use gpl/lgpl to allow open source projects to use the code and other developers to evaluate the library and have a commercial offering with support (if required). You can use mpl to allow full use, demanding a mention of your name/project in the about box /documentation which is like a scientific paper and citations I guess, bsd license for a more liberal use, etc.
Keep in mind that since you are sharing the code and not a DLL/SO lgpl is effectively the same as GPL unless you go with the modified lgpl fpc/lazarus is using.

Personally I stay clear of gpl/lgpl licenses unless I have no other choice.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

VTwin

  • Sr. Member
  • ****
  • Posts: 425
  • Former Turbo Pascal 3 user
Re: Can a type helper on a dynamic array create its own instance ?
« Reply #8 on: July 10, 2018, 02:25:29 am »
Thanks for the tips all. I'm not trying to make money off my code. I've tried, but it is a small market, so I take it as a contribution to my field and colleagues. For that I receive recognition, citations, invitations to give workshops and talks, etc. 

However several companies release similar commercial products, and there are some closed source freeware writers who can't be bothered to cite sources. I believe they would gladly use my code if I released it as LGPL. I don't actually trust them to respect GPL, but at least they would be in violation.

I may release the common libraries as LGPL, but the programs as GPL. I probably overthink it.

Cheers,
VTwin
« Last Edit: July 10, 2018, 02:52:56 am by VTwin »
“Talk is cheap. Show me the code.” Linus Torvalds

Lazarus 1.8.4, FPC 3.0.4
macOS 10.11.5 (32 bit Carbon, working on Cocoa) 
Windows 7 (64 bit)
Ubuntu 16.04.3 (64 bit)

PascalDragon

  • Full Member
  • ***
  • Posts: 134
  • Compiler Developer
Re: Can a type helper on a dynamic array create its own instance ?
« Reply #9 on: July 11, 2018, 11:16:53 pm »
Is it legal to call a type helper to initialise a dynamic array ? In this code, the helper "make" works as intended, although the array has not been set a length yet, and "assigned" returns false. With a Class, that would not be possible. It seems that for dynamic arrays "assigned" is just an alias for length=0, and helper methods can be used anytime.

The methods of a type helper for primitive types or records essentially have the form SomeMethod(var Self: Type; ...), so yes, that is supported (for classes and interfaces that is not the case, they have a by-value Self parameter).

avra

  • Hero Member
  • *****
  • Posts: 1298
    • Additional info
Re: Can a type helper on a dynamic array create its own instance ?
« Reply #10 on: July 12, 2018, 04:05:02 pm »
I may release the common libraries as LGPL, but the programs as GPL. I probably overthink it.
You are free to do whatever you want with your code, but if you release code as pure LGPL instead of FPC modified LGPL with linking exception (as mentioned above) - that would mean that your code licence will be viral to all code that uses your lib, so commercial use will probably be close to zero. If that is what you want - fine, but if not then FPC modified LGPL would allow linking to your lib without request to publish whole code - only changes to your lib.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

 

Recent

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