www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 11733] New: Refuse int indexing of associative arrays with char keys

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

           Summary: Refuse int indexing of associative arrays with char
                    keys
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: accepts-invalid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc


--- Comment #0 from bearophile_hugs eml.cc 2013-12-12 10:11:20 PST ---
I think this code should be disallowed:


void main() {
    int[char] aa1;
    int[dchar] aa2;
    auto r1 = aa1[0];
    auto r2 = aa2[0];
}


dmd 2.065alpha compiles it with no errors, and gives at run-time:

core.exception.RangeError temp(4): Range violation

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 12 2013
next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11733


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |yebblies gmail.com


--- Comment #1 from yebblies <yebblies gmail.com> 2013-12-13 18:01:55 EST ---
Why?  0 implicitly converts to both char and dchar.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 12 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11733


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |minor


--- Comment #2 from bearophile_hugs eml.cc 2013-12-13 04:46:25 PST ---
This is not as important as other changes I have proposed elsewhere.


(In reply to comment #1)
 Why?  0 implicitly converts to both char and dchar.
Because int and char are two different types. It makes code less clear, and in D there is a simple syntax to express that: auto r1 = aa1['\0']; auto r2 = aa2['\0']; There is no ambiguity here, '\0' is clearly a char for the person that reads the code. Using 0 instead of '\0' seems a weakly typing archaism coming from C. -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 13 2013
prev sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11733


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Platform|x86                         |All
         OS/Version|Windows                     |All
           Severity|minor                       |enhancement


--- Comment #3 from yebblies <yebblies gmail.com> 2013-12-14 01:19:24 EST ---
There is no ambiguity anyway.  Asking for int not to convert to char is not
new, and I vaguely recall it was rejected on the grounds that char x = 'a'+7;
should work.  I don't if there is an existing report for that.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 13 2013