Can a type helper on a dynamic array create its own instance ? lazarus / 62-General ---------------------------------------------------------------------- Nitorami · 2018-07-08 19:32 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$modeswitch typehelpers} type TMatrix = array of array of real; type TMatHelper = type helper for TMatrix        procedure Make (dimx,dimy: integer);     end; procedure TMatHelper.Make (dimx,dimy: integer);begin  SetLength (self,dimx,dimy);end;  procedure print (M: TMatrix);var x,y: integer;begin  for x := low(M) to high(M) do begin  for y := low(M[x]) to high(M[x]) do write (m[x,y]:8:3);    writeln;  end;end; var M1: TMatrix;begin  writeln ('Assigned: ',Assigned(M1));  M1.Make (3,3);  Print (M1);  writeln ('Assigned: ',Assigned(M1));end.  Output: QuoteRunning "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 · 2018-07-08 20:10 as expected yes. take a closer look on how is the setlength declared . Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure SetLength(var S: AStringType; Len: SizeInt);procedure SetLength(var A: DynArrayType; Len: SizeInt); 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. ---------------------------------------------------------------------- ASerge · 2018-07-08 20:39 Quote from: Nitorami on July 08, 2018, 09:32:59 pmIs 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 · 2018-07-08 22:52 Wow, I did not realize this. Very cool. So apparently I can rewrite code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i a := Det(b);c := Transpose(b); by using helpers, with: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i a := b.Det;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. ---------------------------------------------------------------------- VTwin · 2018-07-08 23:21 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. ---------------------------------------------------------------------- ASerge · 2018-07-09 05:39 Quote from: VTwin on July 09, 2018, 12:52:41 amWow, 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 · 2018-07-09 09:09 Quote from: VTwin on July 09, 2018, 01:21:52 amI 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 ---------------------------------------------------------------------- taazz · 2018-07-09 10:15 Quote from: VTwin on July 09, 2018, 01:21:52 amAs 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. ---------------------------------------------------------------------- VTwin · 2018-07-10 00:25 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 ---------------------------------------------------------------------- PascalDragon · 2018-07-11 21:16 Quote from: Nitorami on July 08, 2018, 09:32:59 pmIs 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 · 2018-07-12 14:05 Quote from: VTwin on July 10, 2018, 02:25:29 amI 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.