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

Have search throw an exception instead of returning zero/range #20691

Closed
Hydrotoast opened this issue Feb 20, 2017 · 3 comments
Closed

Have search throw an exception instead of returning zero/range #20691

Hydrotoast opened this issue Feb 20, 2017 · 3 comments
Labels
won't change Indicates that work won't continue on an issue or pull request

Comments

@Hydrotoast
Copy link

Hi, I have some thoughts on the edge case return values for the current search API.

Currently, search describes a failure to match as follows:

search(string, "substring") = start:end such that string[start:end] == "substring", or 0:-1 if unmatched.

search(string, 'c') = index such that string[index] == 'c', or 0 if unmatched.

This means that users of this API must handle the case of unmatching with some non-intuitive check. For example:

str = "haystack"
result = search(str, "needle")
if result == 0:-1 # or result[1] == 0 or first(result) == 0
    # handle case where no result was found
else
    # handle case where result was found
end

It turns out that first(result) == 0 is the condition that handles the three cases for when the object to search for (the needle) is either a string, a character, or a vector of characters since the API has two different return types depending on the the needle type.

The proposal here is to instead throw some type of exception e.g. UnmatchedException.

@nalimilan
Copy link
Member

I think raising an exception is not possible because it would kill performance. Note that you can just check for isempty(result). If that's not clear in the manual and in the docstrings, they should be improved.

Regarding the search variant returning a single index, there have been discussions to decide whether they should return a Nullable instead of 0 as currently. See #15755 and the first point of this section of the Find & Search Julep).

@yuyichao yuyichao added the won't change Indicates that work won't continue on an issue or pull request label Feb 20, 2017
@StefanKarpinski
Copy link
Member

I think raising an exception is not possible because it would kill performance.

This is also a matter of language philosophy – we don't use exceptions to indicate unexceptional situations.

@StefanKarpinski
Copy link
Member

StefanKarpinski commented Feb 20, 2017

There are also situations where search* returning an empty range of indices is extremely convenient. For example, inserting into sorted vectors: http://stackoverflow.com/questions/25678112/insert-item-into-a-sorted-list-with-julia-with-and-without-duplicates/25688266.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
won't change Indicates that work won't continue on an issue or pull request
Projects
None yet
Development

No branches or pull requests

5 participants