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

Make it easier to String.slice to the end of a string #1566

Closed
spotandjake opened this issue Jan 1, 2023 · 6 comments · Fixed by #1748
Closed

Make it easier to String.slice to the end of a string #1566

spotandjake opened this issue Jan 1, 2023 · 6 comments · Fixed by #1748
Assignees

Comments

@spotandjake
Copy link
Member

currently if i wanted to do something similar to "test".slice(1) in js to get "est", In Grain the only way to currently do this would be String.slice(1, String.length("test"), "test") == "est" Negative indexing feels like a fix to this but String.slice(1, -1, "test") == "es", My solutiin to this was String.slice(0, -0, "test") == "est" but after asking @ospencer about this in the discord it was noted that we should never ever use -0, One possible solution to this would be to make String.slice use ranges then we could have inclusive or exclusive ranges but this is somewhat blocked by #1404 and would be a breaking change, another possible nonbreaking solution would be adding a String.sliceIndex(<splitIndex>, "string") but that would be less flexible and might get confusing.

TLDR: currently slicing from an index to the end of a string is extremly verbose, it would be nice if it was made simplier.

@alex-snezhko
Copy link
Member

If ranges were to be used instead then wouldn't you still have the same problem of not being able to use a shortcut? For example you'd still have to call String.length for the upper bound of the range

@spotandjake
Copy link
Member Author

Grain has Inclusive And Exclusive ranges. so you could use an inclusive range for that behaviour.

@alex-snezhko
Copy link
Member

Yes but you'd still have to call String.length wouldn't you? It'd either be Range.Inclusive(0, String.length(s) - 1) or Range.Exclusive(0, String.length(s))

@ospencer
Copy link
Member

ospencer commented Jan 1, 2023

Yeah, we haven't really talked about negative indexing on ranges.

@alex-snezhko
Copy link
Member

I suppose a hacky alternative would be to allow Infinity as the end index although that doesn't seem like a great compromise

@phated
Copy link
Member

phated commented Jan 2, 2023

This isn't a problem once we have optional arguments, so I'd vote that we don't "solve" it until then. Once optional arguments exist, the "end" argument will be optional

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Status: Done
Development

Successfully merging a pull request may close this issue.

4 participants