www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 481] New: Letting compiler determine length for fixed-length arrays

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=481

           Summary: Letting compiler determine length for fixed-length
                    arrays
           Product: D
           Version: 0.172
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: wbaxter gmail.com


There needs to be a way to tell the compiler to count up how many elements
there are in a fixed length array.  In C++ you use empty brackets, [], but in D
this means "create a dynamic array".  Sometimes you want a fixed-length array
but it's a pain to count up the elements by hand.  The main example is
embedding binary data, like images, into the program as code.

A possible syntax would be to use the pre-existing '$' or 'length' indicators
to specify that the length of the array should be the length of the
initializer:

   int arr[$] = [1,2,3,4];  // makes an int[4]
   int arr[length] = [1,2,3,4];  // ditto
   int[$] arr = [1,2,3,4];  //  ditto
   int[length] arr = [1,2,3,4];  // ditto

Another option would be to re-use the keyword auto.
   int arr[auto] = [1,2,3,4];  // makes an int[4]
   int[auto] arr = [1,2,3,4];  //  ditto

Using 'auto' makes sense in that the length really is part of the type of a
fixed-length array, so really what you're saying is "automatically deduce just
this part of the type for me".

Using 'auto' would also be extend naturally to associative arrays when/if there
are initializers for those.  For example:

   int[auto] str_to_num = [ "one":1, "two":2, "three":3 ];

This would specify that value type is int, but let the key type be
automatically deduced from the initializer.


-- 
Nov 04 2006
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=481


Andrei Alexandrescu <andrei metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |andrei metalanguage.com
         AssignedTo|nobody puremagic.com        |bugzilla digitalmars.com


--- Comment #1 from Andrei Alexandrescu <andrei metalanguage.com> 2010-11-26
11:36:34 PST ---
This enhancement's importance is raised by the fact that array literals have
dynamic length by default, so simply writing

auto a = [1,2,3,4];

won't make a of type int[4].

I think using "[$]" is the most sensible option. The special meaning of
"length" inside array brackets needs to be eliminated anyway, and "[auto]" may
confuse people into thinking it's an associative array.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 26 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=481


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc


--- Comment #2 from bearophile_hugs eml.cc 2010-11-26 12:24:55 PST ---
I agree that this syntax is good:
int[$] arr = [1,2,3,4];

But it solves only half of the problem.
Currently this compiles:

int[4] a = [1, 2, 3];
void main() {}

While this generates:
object.Exception: lengths don't match for array copy

void main() {
    int[4] a = [1, 2, 3]; 
}

They are two different situations. But they don't look different, the first
just look like a special case.

To solve the second half of the problem someone has suggested this syntax that
looks good enough:
int[4] a = [1, 2, 3, ...];
a[3] is filled with typeof(a[0]).init.

For more info see bug 3849

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 26 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=481


Bruno Medeiros <bdom.pub+deebugz gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bdom.pub+deebugz gmail.com


--- Comment #3 from Bruno Medeiros <bdom.pub+deebugz gmail.com> 2010-12-03
05:28:18 PST ---
(In reply to comment #1)
 This enhancement's importance is raised by the fact that array literals have
 dynamic length by default, so simply writing
 
 auto a = [1,2,3,4];
 
 won't make a of type int[4].
 
 I think using "[$]" is the most sensible option. 

I don't quite get this argument. If you have a syntax such as "[$]" (I assume you are referring to Bill's example of "int[$] arr"), then obviously auto a = [1,2,3,4]; still won't make a of type int[4]... The only way to use auto (in it current form) to declare a static array would be to have a syntax for a static array literal. Or to have a template and/or function to create the static array value. Isn't this latter alternative preferable? A bit more verbose, but no language changes should be necessary. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 03 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=481



--- Comment #4 from bearophile_hugs eml.cc 2010-12-11 00:35:09 PST ---
(In reply to comment #3)
 I don't quite get this argument.

Originally array literals used to create fixed length arrays: auto a = [1,2,3,4]; ==> int[4] Later D was changed so that line creates a dynamic array: auto a = [1,2,3,4]; ==> int[] that's handy, but now if you have a long array literal and you want a fixed-sized array you have to count how many items it has. This is not handy, especially if later you want to add or remove items from the array literal (in the source code). It's not handy also because if by mistake you write a bigger number, empty items are silently created (but only for global fixed-sized literals): int[5] a = [1,2,3,4]; ==> int[5] So Bill and others have suggested a simple syntax that is easy enough to read, understand and remember: int[$] a = [1,2,3,4]; ==> int[4] And to avoid bugs produced by wrong number of items a second syntax may be introduced, that makes it explicit that there are some auto initialized items: int[6] a = [1,2,3,4,...]; ==> int[6] -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 11 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=481



--- Comment #5 from Bruno Medeiros <bdom.pub+deebugz gmail.com> 2010-12-14
04:27:54 PST ---
(In reply to comment #4)
 (In reply to comment #3)
 I don't quite get this argument.

Originally array literals used to create fixed length arrays:

Yes, I understood the use case for this problem, and how the [$] syntax would solve the limitation in a way. But then we're not looking for a syntax that would also allows us to use auto (as in "auto a = [1,2,3,4];"), is that what we're saying? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 14 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=481



--- Comment #6 from bearophile_hugs eml.cc 2010-12-14 13:24:26 PST ---
(In reply to comment #5)

 But then we're not looking for a syntax that would also allows us to use auto
 (as in "auto a = [1,2,3,4];"), is that what we're saying?

Now I think I understand what you mean. You mean something like: auto[$] a = [1,2,3,4]; ==> int[4] So auto lets the D compiler infer the element type and count the items, but the result is a fixed-length array still. I think this case is not common enough to deserve a third syntax. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 14 2010