Recent

Poll

This feature

Cool
13 (50%)
Sucks
13 (50%)

Total Members Voted: 24

Author Topic: Feature idea: array [10] of integer  (Read 16835 times)

v.denis

  • Guest
Feature idea: array [10] of integer
« on: January 29, 2015, 12:51:03 pm »
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.
« Last Edit: January 29, 2015, 01:00:42 pm by v.denis »

airpas

  • Full Member
  • ***
  • Posts: 179
Re: Feature idea: array [10] of integer
« Reply #1 on: January 29, 2015, 01:42:31 pm »
less typing is always good .


BeniBela

  • Hero Member
  • *****
  • Posts: 905
    • homepage
Re: Feature idea: array [10] of integer
« Reply #2 on: January 29, 2015, 01:51:47 pm »
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

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Feature idea: array [10] of integer
« Reply #3 on: January 29, 2015, 02:08:53 pm »
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
« Last Edit: January 29, 2015, 05:33:59 pm by User137 »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Feature idea: array [10] of integer
« Reply #4 on: January 29, 2015, 02:27:02 pm »



Or maybe even

Code: [Select]

{$mode cascal}

const arr: [] of int = {1, 2, 3, 4, 5};

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Feature idea: array [10] of integer
« Reply #5 on: January 29, 2015, 02:51:04 pm »
{$mode cascal}
well, it looks more like {$mode delphi} or even {$mode delphixe}

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Feature idea: array [10] of integer
« Reply #6 on: January 29, 2015, 02:57:00 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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Feature idea: array [10] of integer
« Reply #7 on: January 29, 2015, 03:17:58 pm »
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)
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4459
  • I like bugs.
Re: Feature idea: array [10] of integer
« Reply #8 on: January 29, 2015, 03:38:18 pm »
Code: [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. :)
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Feature idea: array [10] of integer
« Reply #9 on: January 29, 2015, 03:38:41 pm »
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.
« Last Edit: January 29, 2015, 03:42:09 pm by marcov »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Feature idea: array [10] of integer
« Reply #10 on: January 29, 2015, 03:40:14 pm »
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4459
  • I like bugs.
Re: Feature idea: array [10] of integer
« Reply #11 on: January 29, 2015, 03:42:02 pm »
Both 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.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Feature idea: array [10] of integer
« Reply #12 on: January 29, 2015, 03:50:23 pm »
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?
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4459
  • I like bugs.
Re: Feature idea: array [10] of integer
« Reply #13 on: January 29, 2015, 04:04:12 pm »
For 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.

Quote
Letting 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.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Feature idea: array [10] of integer
« Reply #14 on: January 29, 2015, 04:18:05 pm »
Both 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_constructors

 

TinyPortal © 2005-2018