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

String: add sub with index and range #2823

Closed
wants to merge 1 commit into from

Conversation

asterite
Copy link
Member

In Ruby this is String#[]=, which we were missing. Suggestions for alternatives to the name sub are accepted, but maybe sub is good enough.

@oprypin
Copy link
Member

oprypin commented Jun 13, 2016

Well, exactly, why can't this be String#[]=?

@asterite
Copy link
Member Author

Strings are immutable

private def range_to_index_and_size(range)
from = range.begin
from += size if from < 0
raise IndexError.new if from < 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proper error message already pretty please?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include the from in the message, something like #{from} out of bounds?

To be honest, I'm not sure these error messages are very useful. If you get IndexError you have a bug in your program. If you see "3 not in 0..2" you don't get a lot of information to fix the bug just from that info. You'll have to debug it anyway to understand why that 3 got there in the first place.

But I can add a message. What message?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it's not super useful but I think it's useful enough for a lot of cases, I often start to debug them by first printing the input values and boundaries, that step wouldn't be necessary then. But in general it also makes the language appear more friendly, at least it's trying to give as much information as possible, instead of throwing a blunt "wrong" into your face.

I started making all IndexErrors verbose but got stuck on #2819, you can see the message I used here: https://github.com/jhass/crystal/blob/better_index_error/src/exception.cr#L189-L198

@asterite asterite added this to the 0.18.6 milestone Jun 27, 2016
@asterite
Copy link
Member Author

Merged in 7a9dfaf

We can later improve the error messages

@asterite asterite closed this Jun 27, 2016
@asterite asterite deleted the feature/string_sub_index_range branch July 3, 2016 23:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants