www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5034] New: Ranged (or bounded) array initializer

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

           Summary: Ranged (or bounded) array initializer
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Other
            Status: NEW
          Keywords: spec
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: rsinfu gmail.com


--- Comment #0 from Shin Fujishiro <rsinfu gmail.com> 2010-10-10 13:39:45 PDT
---
When creating certain kind of tables, people often do the following brute-force
to fill some ranges in static arrays as follows:


enum CharType { none, digit, alpha, punct }

static immutable CharType[char.max + 1] toCharType =
[
 /* 0 1 2 3 4 5 6 7 8 9 */
    0,0,0,0,0,0,0,0,0,0,
       ...snip...
    0,0,0,0,0,1,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,
       ...snip...
];


It's very bug-prone and hard to maintain.  So, for such kind of range-filling
initialization, I propose the following more clean syntax:


static immutable CharType[char.max + 1] charTypes =
[
    // [new syntax] ranged initializers
    '0' .. '9'+1: CharType.digit,
    'A' .. 'Z'+1: CharType.alpha,
    'a' .. 'z'+1: CharType.alpha,

     // usual initializer
    ':': CharType.punct
];


Does it look good?  As for this problem, I think CTFE doesn't much help - the
above syntax is very clean and intuitive.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 10 2010
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5034



--- Comment #1 from Shin Fujishiro <rsinfu gmail.com> 2010-10-10 13:47:50 PDT
---
A syntax for "filling the rest" would also help.  I don't come up with any good
syntax though.  Maybe something like follows:

int[char.max+1] table =
[
    'a': ALPHA,
    '0': DIGIT,
    ...: UNKNOWN  // fill the rest with UNKNOWN
];

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 10 2010