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

Add String#index/rindex! methods #12730

Merged

Conversation

Sija
Copy link
Contributor

@Sija Sija commented Nov 9, 2022

Adds String#index! and String#index! methods, similar to the ones found in Enumerable.

Supersedes/closes #12729

src/string.cr Outdated Show resolved Hide resolved
Copy link
Member

@beta-ziliani beta-ziliani left a comment

Choose a reason for hiding this comment

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

Just an observation. Thanks Sija! <3

spec/std/string_spec.cr Outdated Show resolved Hide resolved
@asterite
Copy link
Member

IndexError doesn't make sense here. Index error means the index was out of bounds. That's not the case here.

@Sija
Copy link
Contributor Author

Sija commented Nov 10, 2022

@asterite Any ideas for a better ones?

@asterite
Copy link
Member

asterite commented Nov 10, 2022

Enumerable::NotFoundError

@Sija
Copy link
Contributor Author

Sija commented Nov 10, 2022

@asterite I have a bit of an issue with this, since String is not an Enumerable, technically speaking.

@asterite
Copy link
Member

Then NotFoundError should be moved to the top level.

@asterite
Copy link
Member

Just like IndexError, KeyError, etc., are all at the top level. We can probably keep the ones in Enumerable as an alias of the top level ones.

Just my opinion though! I'm sure others will propose more complex solutions.

@Sija
Copy link
Contributor Author

Sija commented Nov 10, 2022

@asterite NotFoundError in the top-level is gonna be IMO too generic, or maybe not...?

@straight-shoota
Copy link
Member

Let's just use Enumerable::NotFoundError here as we do in Enumerable#index!.
Move that type to the top-level namespace should be a separate discussion.

@asterite
Copy link
Member

Why NotFoundError would be too generic, but IndexError or KeyError would not? Maybe it could be called ElementNotFoundElement or something like that.

Another idea: just use whatever .not_nil! does for now.

@Sija Sija force-pushed the add-string-index-bang-variant branch from 7d1648b to 6ddb1eb Compare November 10, 2022 15:28
src/string.cr Outdated Show resolved Hide resolved
src/string.cr Outdated Show resolved Hide resolved
@Sija Sija force-pushed the add-string-index-bang-variant branch from 5b973f8 to 6362c57 Compare November 10, 2022 18:10
@Sija
Copy link
Contributor Author

Sija commented Nov 10, 2022

@beta-ziliani Good catch, fixed!

Copy link
Member

@sdogruyol sdogruyol left a comment

Choose a reason for hiding this comment

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

Thank you @Sija 🙏

@beta-ziliani beta-ziliani added this to the 1.7.0 milestone Nov 10, 2022
@HertzDevil
Copy link
Contributor

#rindex! is uncalled for; #11566 didn't add it to the standard library and Ameba does not warn on it. It would be odd to have it here before the collection types do.

@straight-shoota
Copy link
Member

IMO #rindex! makes as much sense as #index!, so there would be no harm in adding it. We should add it to Enumerable in the same manner.

@straight-shoota straight-shoota removed this from the 1.7.0 milestone Nov 14, 2022
@HertzDevil
Copy link
Contributor

I fully agree with adding #rindex!, I just think it should be organized into:

  • String#index!
  • #rindex! on everything

rather than:

  • String#index! + String#rindex!
  • #rindex! on everything else

@straight-shoota
Copy link
Member

Oh, okay. "#rindex! is uncalled for;" reads like you wouldn't approve of it whatsoever.

IMO the organizational aspects don't matter much. String#index! and String#rindex! are related and we have a ready-to-go PR for them. I see little point in moving things around into different PRs.

@straight-shoota straight-shoota added this to the 1.7.0 milestone Nov 18, 2022
@straight-shoota straight-shoota merged commit 762cdd6 into crystal-lang:master Nov 20, 2022
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.

6 participants