-
Notifications
You must be signed in to change notification settings - Fork 71
Suggestion: allow slice (length s) (length s)
on Strings
#143
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
Comments
One downside to accepting this PR is that the changes would be silent. What returned However, I don't think this design was a mistake. This is what I get when I run this in my browser console:
|
I don't quite understand your comment. By silent you mean that it could silently break existing code? That's a fair point; it would be a reverse-incompatible change, and I understand if it's rejected for that reason. Yeah, |
Silent in the sense that the compiler doesn't issue an error or otherwise notify you that this function no longer works the same as it did before. Ah, I didn't know that |
Yeah, that's totally a valid concern. A fair option would be to provided a modified JS |
I don't really have a good answer to deal with the potential breakage here, but I'd agree that this being "out of bounds" is a mistake. An unfortunate legacy I introduced from the early days of this library... I think there may be some older issues discussing it previously where I argued against it, but I think I was wrong to do so now. Especially since the advent of One way to make it "obviously breaking" would be to remove the |
I had a similar complaint with a different function back in #78 |
Thanks for the link! I guess I ducked that conversation 😉 |
I think the easiest fix is to add Thoughts? |
Very 👎 for adding a
This is probably one of the most important libraries in core, and so I think it's vital that we take a lot of care over the API surface we expose. We definitely shouldn't just do what's easiest; having a I think the conclusion we reached in #78 was the correct one (i.e. that not returning Nothing for out of bounds is best) but I have one regret in how we made that change, in that we didn't go over the whole API to ensure that our approach to out-of-bounds cases for functions that do slicing is consistent throughout. If we had, we probably would have made this change then as well. If/when we do this I think it's crucial that we go over the whole API to make sure that everything is consistent in how it handles out-of-bounds cases, or at least as consistent as we can reasonably make it. |
How about:
|
I think we should consider this ticket as your point 1, and I think we shouldn't do your point 2; it's not compatible with what I was saying:
The situation is marginally improved by having better names but it's still very much not ideal in my mind. |
If you're intent on having one true |
The reason I'm unconvinced by Maybes is that we've had a few discussions about out-of-bounds slicing over the years and, as far as I know, not once has anyone been able to give a persuasive concrete example of where the Maybe behaviour is useful. |
Yeah, that's why I changed my mind on it too. I'm pretty sure any case where it would have been useful will be done under |
Makes sense. (I feel like I have in the past had use-cases for On the topic of consistency and cohesion, should the normalization of negative indices even be performed? Similar functions ( Regardless, I've created a WIP PR for this ticket. |
Currently,
Data.String.CodeUnits.slice
returnsNothing
on slices from the final index to the final index, e.g.I find this behavior to be undesirable. It:
i
from0
throughlength s
, we haves == slice 0 i s <> slice i (length s)
. Instead, this only holds up tolength s - 1
.Motivating example: I was dealing with string containing two parts separated by a delimiter, like
prefix:::suffix
. I had a function to separate the two parts,getParts :: String -> Maybe (String /\ String)
, which was implemented viaindexOf
andsplit
. With the currentsplit
implementation, this function has thatgetParts ":::suffix" == Just ("" /\ "suffix")
butgetParts "prefix:::" == Nothing
, which is surprising.Happy to make an MR if it's decided that this is reasonable. I suppose it's technically a reverse-incompatible change, though.
The text was updated successfully, but these errors were encountered: