[New feature request - Static array declaration improvement]

[New feature request - Static array declaration improvement]
https://quality.embarcadero.com/browse/RSP-16635

A lot of time I struggle with this:

LArray: array[0..SOME_CONSTANT - 1] of Int8;

I can see this language construction a lot, especially when dealing with POSIX APIs that have a bunch of constants. I would love to be able to write this in the syntax below:

LArray: array[0.
It means we have the range of values from 0 til SOME_CONSTANT - 1, or more generally, from start inclusive to end exclusive. This looks more expressive to me :D
https://quality.embarcadero.com/browse/RSP-16635

Comments

  1. Personally, I would like to see

    LArray: array[SOME_CONSTANT] of Int8;

    Which would be a shorthand for

    LArray: array[0..SOME_CONSTANT - 1] of Int8;

    ReplyDelete
  2. > This looks more expressive to me

    But I think it would look very confusing to the majority of the Delphi developers.

    ReplyDelete
  3. Very confusing. "< SC" implies "SC - 1"?

    ReplyDelete
  4. Marco Cantù Perhaps something like

    LArray: array[RangeOf(SOME_CONSTANT)] of Int8;

    would satisfy both needs (the similarity to SizeOf is intented).

    ReplyDelete
  5. Uwe Raabe Maybe. I think my issue is I don't find the current declaration so bad and the new one will not really work outside of numeric based arrays (while static arrays can be based on any ordinal type, a rather unique feature of Pascal).
    More over, I think we should push developers towards dynamic arrays as the default.

    ReplyDelete
  6. Marco Cantù Why on earth make unnecessary heap allocations? Blind use of dynamic arrays (in particular TBytes) also leads to bugs for the unwary because the details of how they work aren't always obvious + they aren't actually any 'safer' than alternatives.

    ReplyDelete
  7. Marco Cantù​, and the unnecessary calls to SetLength, especially if you want the array in a local scope (for faster access and proper encapsulation) in a procedure and that procedure is called in a loop?
    Hell No.

    I recently refactored a code base to use static arrays in places I know the array size before hand and got a huge speed boost, at least 2X speed improvements.

    ReplyDelete
  8. Personally I would prefer if we could just write
    LArray: array[] of Int8 = (....);

    and have the compiler figure out the correct length.

    ReplyDelete
  9. Marco Cantù Static arrays have their place, as do dynamic arrays. Should it be more of what the community wants and not what you want (as that is how I interpret your reply - if this in correct, I do apologise).

    Changing the compiler to accept the syntax I have suggested should be (I guess), "reasonable easy", as it already knows to throw an error where there is no ".." between the square brackets.

    ReplyDelete
  10. Asbjørn Heid Actually you can write

    LArray: TArray = [1, 2, 3];

    as well as

    LArray: Array of Int8 = [1, 2, 3];

    ReplyDelete
  11. I know static arrays have a role, I wasn't clear in my comment. I'm not much keen on changing how they work and have been working in Pascal since the early days.

    Using a value for the range, causes the dilemma on 1- based vs. 0-based. And a bigger issue for non numerical ranges.

    I know integer-based array index is the only option in most languages, but Pascal has more flexibility there, worth keeping

    ReplyDelete
  12. Uwe Raabe Then you'll get a dynamic array, which is not equivalent in many contexts.

    ReplyDelete
  13. I'm with Marco on this, it will be easy to confuse this declaration with arrays with non numerical ranges. Static arrays are mostly needed for old API calls, most new APIs require you to dynamically change data size if it does not fit.

    ReplyDelete
  14. My usage of static arrays is restricted to calling native APIs, one of the things I extremely like in Delphi is the easy integration with the native side of the target platform. When it is still very common dealing with native APIs for example: POSIX, JNI, macOS libraries, dynamic arrays doesn't fit for them and still important. Please improve the static array declaration syntax, if a pattern (0..SOME_CONSTANT - 1) repeats a lot it is signal of a bad design :(

    ReplyDelete
  15. At work, I have inherited an old application (that is more in maintenance mode than development mode) that has a lot (we are talking dozens and dozens) of constant static string arrays that defined as:

    const
    SomeThing : array[0..2] of string = ('1=this', '2=that', '3=something else');

    And would it be lovely to just declare the array to be:

    SomeThing : array[3] of string = ('1=this', '2=that', '3=something else');

    Eugene Kotlyarov So what is so confusing with the later?

    ReplyDelete
  16. Nicholas Ring The confusing thing with your example is that it is not immediately clear what the array range actually is. Are you really saying that array[3] is more readable than array[0..2]? array[3] implies a zero based index, which is not mandatory in Delphi. It can as well be interpreted as array[1..3]. Relying on a hidden convention seems not adequate in this case. The obfuscation done by this shortening is worse than the little benefit it brings.

    ReplyDelete
  17. Uwe Raabe What about the hidden convention of SetLength(myVar, 10)?
    If "myVar" is declared as an array, then it is 0..9, but if 'myVar' is a string, then it is myVar[1].. myVar[10] (the compiler will warn you if you try myVar[0]). Of cause, we now can also bring in zero-based strings...

    ReplyDelete
  18. Nicholas Ring For constants it is Ok, although I would prefer to not have number at all, like SomeThing : array of string = ('1=this', '2=that', '3=something else'); It is mostly for variables when you have some numeric constant and declare as array [0..some_constant] of string

    ReplyDelete
  19. Eugene Kotlyarov So you need to have an array of 'some_constant' + 1 in length? That does seem confusing to me (that you need an array one more than the constant you are using), and not to mention error prone ((IMO).

    ReplyDelete
  20. Eugene Kotlyarov As I mentioned earlier: SomeThing : array of string = ('1=this', '2=that', '3=something else'); is already a valid construct for constants as well as for global variables. Although this gives a dynamic array, it probably fits for most scenarios.

    ReplyDelete
  21. Attila Kovacs I am not sure of it ;) 😂 Happy new year and don't want my problems I don't even know if they are really problems at all.

    ReplyDelete

Post a Comment