[Issue 481] Letting compiler determine length for fixed-length arrays

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Dec 11 00:36:53 PST 2010


http://d.puremagic.com/issues/show_bug.cgi?id=481



--- Comment #4 from bearophile_hugs at 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: -------


More information about the Digitalmars-d-bugs mailing list