Bug 5034 – Ranged (or bounded) array initializer

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Other
Creation time
2010-10-10T13:39:45Z
Last change time
2024-12-13T17:53:54Z
Keywords
spec
Assigned to
No Owner
Creator
Shin Fujishiro
Moved to GitHub: dmd#18311 →

Comments

Comment #0 by rsinfu — 2010-10-10T13:39:45Z
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.
Comment #1 by rsinfu — 2010-10-10T13:47:50Z
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 ];
Comment #2 by johannes.loher — 2018-05-05T12:39:45Z
I believe this would definitely need a DIP.
Comment #3 by greeenify — 2018-05-06T03:00:41Z
We will have staticArray in Phobos soon which will greatly help in doing this purely as a library. Though this obviously could already been done easily as a library today. This is definitely very unlikely to end up in the language.
Comment #4 by robert.schadek — 2024-12-13T17:53:54Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18311 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB