[SOLVED] No interest in expansion of array (or set)? lazarus / 64-FPC development ---------------------------------------------------------------------- egsuh · 2021-07-29 00:42 Basically I'm talking about expansions done in javascript.  Some examples are: 1) Array.prototype.join()    Example:       const elements = ['Fire', 'Air', 'Water'];       console.log(elements.join());       // expected output: "Fire,Air,Water" 2) Spread operator (...)     Example:       const arr1 = [1, 2, 3];       const arr2 = [4, 5, 6];       const arr3 = [7, 8, 9];       const arrWrap = [...arr1, ...arr2, ...arr3]; 3) IndexOf or Set.prototype.has(). I think if expanding set operation is not easy, then it may be added to array.  Like TArray.has(). BTW, can I write some of these functions using type helpers in Free Pascal? ---------------------------------------------------------------------- PascalDragon · 2021-07-29 07:15 Quote from: egsuh on July 29, 2021, 02:42:33 am1) Array.prototype.join()    Example:       const elements = ['Fire', 'Air', 'Water'];       console.log(elements.join());       // expected output: "Fire,Air,Water" Not everything is convertible to a string, so this can't be generalized. Quote from: egsuh on July 29, 2021, 02:42:33 am2) Spread operator (...)     Example:       const arr1 = [1, 2, 3];       const arr2 = [4, 5, 6];       const arr3 = [7, 8, 9];       const arrWrap = [...arr1, ...arr2, ...arr3]; And what does the result look like? Quote from: egsuh on July 29, 2021, 02:42:33 am3) IndexOf or Set.prototype.has(). I think if expanding set operation is not easy, then it may be added to array.  Like TArray.has(). This is one of the main features of sets, the in-operator, no need to extend anything there. And arrays are not sets. You can achieve this functionality by declaring a generic function and with the upcoming implicit specialization feature (not yet part of trunk) you even won't have to type out the specialization. Quote from: egsuh on July 29, 2021, 02:42:33 amBTW, can I write some of these functions using type helpers in Free Pascal? You need to declare a type helper for each array type (generic type helpers are not yet supported). Also there might be problems if you try to invoke a helper on an anonymous array of X if it was declared on a named type TXArray = array of X and vice versa (have not tested that part in a while). ---------------------------------------------------------------------- AlexTP · 2021-07-29 10:52 Quote2) Spread operator (...)     Example:       const arr1 = [1, 2, 3];       const arr2 = [4, 5, 6];       const arr3 = [7, 8, 9];       const arrWrap = [...arr1, ...arr2, ...arr3]; No, it looks not Pascalish. Pascalish is: arrWrap = Concat(arr1, arr2, arr3) or: arrWrap = arr1 + arr2 + arr3 ---------------------------------------------------------------------- egsuh · 2021-07-30 23:27 I have tested following, which is good enough. By declaring new types, e.g. TIntegerArray=array of integer, the codes look much like OOP. 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 interface type    TIntegerArray = array of integer;    { HIntegerArray }    HIntegerArray = type helper for TIntegerArray   private      function getCommaText: string;      procedure setCommaText(AValue:string);   public      property CommaText: string read getCommaText write setCommaText;   end; implementation function HIntegerArray.getCommaText: string;var   i: integer;begin   Result:= '';   for i in Self do       Result += IfThen(Result='', '', ',') + IntToStr(i);end; procedure HIntegerArray.setCommaText(AValue: string);var   sarr: TStringArray;   i : integer;begin   if trim(AValue) = '' then Exit;   sarr:= AValue.Split([',']);   SetLength(Self, Length(sarr));   for i:= Low(sarr) to High(sarr) do     Self[i] := StrToInt(sarr[i]);end; // usage procedure TForm2.Button2Click(Sender: TObject);begin    IArray.Commatext:= '3,4,9,10';    IArray += [56];    ShowMessage(IArray.Commatext);end; Well, IArray.Commatext looks much better than    function CommaText(arr: array of integer); ---------------------------------------------------------------------- winni · 2021-07-31 08:45 Hi 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 TIntegerDynArray = array of Integer;  is already defined in the unit types. Winni ---------------------------------------------------------------------- glorfin · 2021-09-16 15:27 Quote from: egsuh on July 31, 2021, 01:27:39 amWell, IArray.Commatext looks much better than    function CommaText(arr: array of integer); Why? I honestly do not understand why everywhere OOP must be used. And one potential problem with this approach is that only one helper at a time may be active. It means, that if several type helpers exist for TIntegerArray, you may end up with not knowing, which one works in a program using many third party units and what behaviour to expect. Therefore, I am not very enthusiastic about type helpers, and to that end about syntax expansion in general. ---------------------------------------------------------------------- PascalDragon · 2021-09-20 07:43 Quote from: glorfin on September 16, 2021, 05:27:15 pmI honestly do not understand why everywhere OOP must be used. While only a small effect it allows an IDE to show the methods currently available while for the procedural approach you need to search through a larger list. Quote from: glorfin on September 16, 2021, 05:27:15 pmAnd one potential problem with this approach is that only one helper at a time may be active. FPC 3.3.1 has the MultiHelpers modeswitch. If multiple helpers provide the same method the order of the units determines which one is used just like for any scoping rules.