[Issue 481] New: Letting compiler determine length for fixed-length arrays
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sat Nov 4 21:49:09 PST 2006
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 at digitalmars.com
ReportedBy: wbaxter at 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.
--
More information about the Digitalmars-d-bugs
mailing list