Feature idea: array [10] of integer lazarus / 64-FPC development ---------------------------------------------------------------------- v.denis · 2015-01-29 11:51 I've just created patch http://bugs.freepascal.org/view.php?id=27377 for declaring array range in C way with just count of elements. Code: [Select]type   // low is implicit 0, high is 9   TArr1 = array[10] of integer; This topic is if someone want to say something. I personally don't see a big problem as it seems it is not breaking code. It just offers one more way to declare array. ---------------------------------------------------------------------- airpas · 2015-01-29 12:42 less typing is always good . ---------------------------------------------------------------------- BeniBela · 2015-01-29 12:51 Cool! Hope it does not derail this thread, but I have another wish. Code: [Select]    const arr: array[] of integer = (1, 2, 3, 4, 5);   //length would be infered to be 5, with low 0, high 4 ---------------------------------------------------------------------- User137 · 2015-01-29 13:08 Both static and dynamic arrays too? Code: [Select]// Static const arr: array[] of integer = (1, 2, 3, 4, 5); var arr: array[] of integer = (1, 2, 3, 4, 5); // Dynamic (can change size later) var arr: array of integer = (1, 2, 3, 4, 5); edit: typo ---------------------------------------------------------------------- marcov · 2015-01-29 13:27 Or maybe even Code: [Select] {$mode cascal} const arr: [] of int = {1, 2, 3, 4, 5}; ---------------------------------------------------------------------- skalogryz · 2015-01-29 13:51 Quote from: marcov on January 29, 2015, 02:27:02 pm{$mode cascal} well, it looks more like {$mode delphi} or even {$mode delphixe} ---------------------------------------------------------------------- marcov · 2015-01-29 13:57 Quote from: skalogryz on January 29, 2015, 02:51:04 pmQuote from: marcov on January 29, 2015, 02:27:02 pm{$mode cascal} well, it looks more like {$mode delphi} or even {$mode delphixe} I don't see anything imported from curly braces languages in that url. It is recycling of tried an true Pascal (set) syntax. (of course as a feature it looks sad, but I don't know why they introduced it) ---------------------------------------------------------------------- skalogryz · 2015-01-29 14:17 Quote from: marcov on January 29, 2015, 02:57:00 pmI don't see anything imported from curly braces languages in that url. It is recycling of tried an true Pascal (set) syntax. (of course as a feature it looks sad, but I don't know why they introduced it) Well, I wanted to make a different point. The patch above would be rejected and another patch should be introduced. The reason for the second patch is "Delphi-compatibility". These kind of patches are more easily accepted, than just new syntax patches. ---------------------------------------------------------------------- JuhaManninen · 2015-01-29 14:38 Quote from: BeniBela on January 29, 2015, 01:51:47 pmCode: [Select]    const arr: array[] of integer = (1, 2, 3, 4, 5);   //length would be infered to be 5, with low 0, high 4 This one would be truly useful. I remember suggesting it at 2009 or 2010 but it was rejected immediately. I have a related feature request still open for Codetools :   http://bugs.freepascal.org/view.php?id=15548 I feel absolutely dummy when I have to count the elements. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ..., 55, 56, 57. I remember the compiler then says something like :  "There are 59 elements, not 57. Why did you give me a wrong number? Shame on you!" So, if the compiler is clever enough to count the elements, why can't it use that count? Why me, a stupid human being, must count them for it? I understand the original array syntax request will be rejected because it only saves you typing 3 characters "0.." which is not very relevant. This const array syntax however would be a big improvement. Even Marcov cannot deny it. ---------------------------------------------------------------------- marcov · 2015-01-29 14:38 Ah, I missed the initialization of the TBytes, and was more looking at the operator aspect. (I thought it was preparation for an "array" generic constraint or so for strings and arrays) True, that syntax is similar (though for the dynamic array only it seems) Juha: one can micromanage syntax ad infinitum, but this is IMHO design-my-own-language hobby, and doesn't really make engineering software any easier. It only makes compiler maintenance harder. There are several suggestions for extensions that actually make things possible that are not possible now. Typing is not interesting. ---------------------------------------------------------------------- Leledumbo · 2015-01-29 14:40 Delphi XE features look nice, though in the real code I don't think I'll ever need most of them. Dynamic array initialization (not with TDynArrayType.Create) is probably the one I'll need the most. ---------------------------------------------------------------------- JuhaManninen · 2015-01-29 14:42 Quote from: User137 on January 29, 2015, 02:08:53 pmBoth static and dynamic arrays too? Code: [Select]// Static const arr: array[] of integer = (1, 2, 3, 4, 5); // Dynamic (can change size later) const arr: array of integer = (1, 2, 3, 4, 5); Uhh... How can const array be dynamic. "Const" by definition is the opposite of dynamic. ---------------------------------------------------------------------- Leledumbo · 2015-01-29 14:50 Quote from: JuhaManninen on January 29, 2015, 03:38:18 pmSo, if the compiler is clever enough to count the elements, why can't it use that count? Why me, a stupid human being, must count them for it? For me, this actually minimizes human error. Most of the time we know both the number of elements and the elements themselves. Forcing the number of elements to be typed prevents us from mistyping some of the elements. Well, if you have 3, 64, 21, 77, 23, 86, 32, 9, 11, 0, 12, 55 (and longer if necessary). Can you ensure that you don't miss any of the element? What's easy for most people is to remember just the number of the elements. So we type on both sides and the compiler will help us ensuring that they're consistent. Letting the compiler guess the number of elements will let us do wrong algorithm if the algorithm relies on the it, e.g. writing a [de]compressor for lossy format will likely suffer from this problem. Thankfully, it's not. The compiler is kind enough to tell you if both sides aren't consistent. ---------------------------------------------------------------------- JuhaManninen · 2015-01-29 15:04 Quote from: Leledumbo on January 29, 2015, 03:50:23 pmFor me, this actually minimizes human error. Most of the time we know both the number of elements and the elements themselves. It depends on a use case. I remember that most often I would happily leave counting for the compiler. QuoteLetting the compiler guess the number of elements ... Count, not guess. I believe both syntax are useful. Fortunately they don't conflict with each other and the syntax could be extended without problems. ---------------------------------------------------------------------- howardpc · 2015-01-29 15:18 Quote from: JuhaManninen on January 29, 2015, 03:42:02 pmQuote from: User137 on January 29, 2015, 02:08:53 pmBoth static and dynamic arrays too? Code: [Select]// Static const arr: array[] of integer = (1, 2, 3, 4, 5); // Dynamic (can change size later) const arr: array of integer = (1, 2, 3, 4, 5); Uhh... How can const array be dynamic. "Const" by definition is the opposite of dynamic. Except that Delphi long ago allowed writeable constants, which sort of redefined "const". FPC now has support (a la Delphi) for initialising dynamic arrays: http://wiki.freepascal.org/FPC_New_Features_Trunk#Dynamic_array_constr uctors ---------------------------------------------------------------------- Martin_fr · 2015-01-29 16:02 Quote from: JuhaManninen on January 29, 2015, 03:38:18 pmThis one would be truly useful. I remember suggesting it at 2009 or 2010 but it was rejected immediately. I have a related feature request still open for Codetools :   http://bugs.freepascal.org/view.php?id=15548 I feel absolutely dummy when I have to count the elements. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ..., 55, 56, 57. Add the following macro (and refine ,or fix (only tested basics). Place caret between at [0..|] (where the total count should and innvoke the macro. Code: [Select]var   porigin, p1, p2: TPoint;   s: string;   i, c1,c2, b: integer;   t: boolean;   a : char; begin   porigin := Caller.CaretXy;   ecIncrementalFind;   ecChar('(');   ecLeft;   i := 1;   c1 := 0;   c2 := 0;   b := 0;   t := false;   repeat     p1 := Caller.CaretXy;     ecRight;     s := Caller.LineAtCaret;     p2 := Caller.CaretXy;     if (p1.y = p2.y) and (p1.x = p2.x) then break;     if (p2.x > length(s)) then begin       t := false; // string ends in line       ecdown;       p2 := Caller.CaretXy;       if (p1.y = p2.y) then break;  // last line       s := Caller.LineAtCaret;       while s = '' do begin         p1 := Caller.CaretXy;         ecdown;         p2 := Caller.CaretXy;         if (p1.y = p2.y) then break;  // last line         s := Caller.LineAtCaret;       end;       if s = '' then break;       Caller.CaretX := 1;       p2 := Caller.CaretXy;     end;     if copy (s, p2.x, 2) = '//' then begin       Caller.CaretX := length(s)+1;       continue;     end;     a := s[p2.x];     if copy (s, p2.x, 2) = '(*' then  a := '<';     if copy (s, p2.x, 2) = '*)' then  a := '>';     if (t) and (copy (s, p2.x, 2) = '''''') then begin       a := ' ';       ecRight;;     end;     case a of       '{': if (c2 = 0) and (not t) then c1 := c1 + 1;       '}': if (c2 = 0) and (c1 > 0) and (not t) then c1 := c1 - 1;       '<': if (c1 = 0) and (not t) then c2 := c2 + 1;       '>': if (c1 = 0) and (c2 > 0) and (not t) then c2 := c2 - 1;       '''': if (c1 = 0) and (c2 = 0) then t := not t;       '(', '[': if (c1 = 0) and (c2 = 0) and (not t) then b := b + 1;       ')', ']': if (c1 = 0) and (c2 = 0) and (not t) then begin           b := b - 1;           if b < 0 then break;         end;       ',': if (c1 = 0) and (c2 = 0) and (not t) then  i := i + 1;     end;   until false;   Caller.CaretXy := porigin;   Caller.InsertTextAtCaret(inttostr(i), scamAdjust ) end. Edit [2]: Added dealing with strings. You may want to limit how far to search if the closing ")" is missing. Or abort when finding a "begin" keyword. And: It does not deal with comments before the '('. It goes to the firsn "(" that it finds, comment or not. ---------------------------------------------------------------------- User137 · 2015-01-29 16:30 Quote from: JuhaManninen on January 29, 2015, 03:42:02 pmUhh... How can const array be dynamic. "Const" by definition is the opposite of dynamic. Uhh.. i didn't see that const at the beginning. Typo fixed: Code: [Select]// Static const arr: array[] of integer = (1, 2, 3, 4, 5); var arr: array[] of integer = (1, 2, 3, 4, 5); // Dynamic (can change size later) var arr: array of integer = (1, 2, 3, 4, 5); ---------------------------------------------------------------------- A.S. · 2015-01-29 18:26 Pascal expects ordinal type of index there but "10" is integer constant. In C it means "the number of elements", not the "type of index". Not sure that it is a good idea. ---------------------------------------------------------------------- Blaazen · 2015-01-29 18:39 This already works: Code: [Select]type   TEnum = (eOne, eTwo, eThree);   TRange = 0..9; var   ArrE: array[TEnum] of SmallInt;   ArrR: array[TRange] of SmallInt; ---------------------------------------------------------------------- JuhaManninen · 2015-01-30 12:11 Quote from: Martin_fr on January 29, 2015, 05:02:05 pmAdd the following macro (and refine ,or fix (only tested basics). Place caret between at [0..|] (where the total count should and innvoke the macro. ... I opened View -> Editor Macros ... I recorded a dummy macro, then Edit, then replaced it with your macro from clipboard. Defined a shortcut, placed the cursor in right spot and run it. Was it the right thing to do? There could be one more button "Create from Clipboard". It works somehow, this is a good example. I must study making complex Editor macros, I have only tested with simple ones so far. Is there any way to debug the macro? I guess not. ---------------------------------------------------------------------- Basile B. · 2015-01-30 13:17 Quote from: Blaazen on January 29, 2015, 07:39:30 pmThis already works: Code: [Select]type   TEnum = (eOne, eTwo, eThree);   TRange = 0..9; var   ArrE: array[TEnum] of SmallInt;   ArrR: array[TRange] of SmallInt; +1 very true, The "enum-indexed-array" is one of my favorite Pascal feature, I use it all the time. ---------------------------------------------------------------------- Martin_fr · 2015-01-30 13:29 Quote from: JuhaManninen on January 30, 2015, 01:11:59 pmI opened View -> Editor Macros ... I recorded a dummy macro, then Edit, then replaced it with your macro from clipboard. Defined a shortcut, placed the cursor in right spot and run it. Was it the right thing to do? There could be one more button "Create from Clipboard". It works somehow, this is a good example. I must study making complex Editor macros, I have only tested with simple ones so far. Is there any way to debug the macro? I guess not. Yes that is the way. And yes "create new (empty)" is needed. I wrote the macro "quick and dirty". It has a lot of potential to clean up and optimize. Instead of all the ecRight, one could just iterate over the entire line string in a for loop. ---------------------------------------------------------------------- moskalenco_a · 2015-02-27 23:16 Maybe make arrays totally like in C?! And types like in C. And all like in C. Code: [Select]int a,b; begin a:=4; b:=4;//Pascal with C++ it's something strange a:=a/b; end. ---------------------------------------------------------------------- Septe · 2015-04-24 03:44 Is the ide capable of counting?  Or adding an extension to the ide?  We could highlight the elements, hit the ide element count button, it counts the item between the commas and give you a count?  I'm still learning Lazarus and FPC so I have no clue what's possible, but, this way we can add a little thing to the ide instead of the compiler. ---------------------------------------------------------------------- Martin_fr · 2015-04-24 03:53 Quote from: Septe on April 24, 2015, 05:44:36 amIs the ide capable of counting?  Or adding an extension to the ide?  We could highlight the elements, hit the ide element count button, it counts the item between the commas and give you a count?  I'm still learning Lazarus and FPC so I have no clue what's possible, but, this way we can add a little thing to the ide instead of the compiler. Yes Pascal Script macros can be added in the IDE (Lazarus) and do it. I gave an example earlier, but it needs still a few fixes.