Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refuse int indexing of associative arrays with char keys #18738

Closed
dlangBugzillaToGithub opened this issue Dec 12, 2013 · 3 comments
Closed

Refuse int indexing of associative arrays with char keys #18738

dlangBugzillaToGithub opened this issue Dec 12, 2013 · 3 comments

Comments

@dlangBugzillaToGithub
Copy link

bearophile_hugs reported this on 2013-12-12T10:11:20Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=11733

CC List

  • yebblies

Description

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
@dlangBugzillaToGithub
Copy link
Author

yebblies commented on 2013-12-12T23:01:55Z

Why?  0 implicitly converts to both char and dchar.

@dlangBugzillaToGithub
Copy link
Author

bearophile_hugs commented on 2013-12-13T04:46:25Z

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.

@dlangBugzillaToGithub
Copy link
Author

yebblies commented on 2013-12-13T06:19:24Z

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants