Stream: ideas

Topic: Str.replace and returning Result


view this post on Zulip Richard Feldman (Jul 28 2023 at 01:09):

so right now Str.replaceFirst and Str.replaceEach return Result Str [NotFound].

in using them recently, I've found the Result annoying and unhelpful, and I think we should change them both to return Str.

view this post on Zulip Richard Feldman (Jul 28 2023 at 01:09):

anyone have any thoughts on that?

view this post on Zulip Ayaz Hafiz (Jul 28 2023 at 01:30):

seems like the right change to me. i didn’t even know they returned results before. this change would like help the optimizer in the common case too, since presumably most often you want to return the string as-is in the not found case, and the optimizer would be able to see the string is unique

view this post on Zulip Pearce Keesling (Jul 28 2023 at 12:48):

Could it return a tuple of the new string and an outcome descriptor? For replace first it'd probably be a tag indicating whether a change was made and for replace all the number of replacements?

view this post on Zulip Anton (Jul 28 2023 at 14:20):

It would be a better alternative than the Result for sure, but I wonder if the second field in the Tuple would be used enough to justify the "noise" caused by the extra code.

view this post on Zulip Pearce Keesling (Jul 28 2023 at 14:40):

Yeah, I guess the case I would hope to avoid is having to process the string twice, once to get the count and once to make the change

view this post on Zulip Pearce Keesling (Jul 28 2023 at 14:41):

But there could be an alternative function that captures that use case because I agree it is probably less likely

view this post on Zulip Richard Feldman (Jul 28 2023 at 16:04):

yeah I'd certainly be open to adding another function with a different name if there's demand for it!


Last updated: Jun 16 2026 at 16:19 UTC