[erlang-questions] string:substr/2 gives confusing error message

Robert Virding <>
Fri Dec 5 16:18:33 CET 2008


Thinking some more and checking the code. Now substr/2 is defined as

    substr(String, StartPos) -> String.

so substr returns the rest of string starting at StartPos. Logically this
would mean that

    string:substr("a",2) -> []

is perfectly ok while

    string:substr("",2)

should generate an error as there is no string starting at the second
element in the string.

Looking at substr/3 which is defined as

    substr(String, StartPos, Length) -> String.

then it should have the same error returns for StartPos as for substr/2. The
remaining question is then how it should handle strings which are too short,
i.e. where there is no rest of length Length. The hard line would be to
bomb, but I see in the code that this is where I was nice and just returned
what there was if there wasn't enough. If this is the best I don't know.

But I do think that

    string:substr("",2) -> []

is being to kind and should generate an error. There's a bug in the code
which I would fix. Of course who knows how amny apps will break because of
this. :-)

Robert

2008/12/5 Valentin Micic <>

>  The way I see it, you're doing a right thing – returning an empty string.
> It is just a matter of covering another pattern: substr( [], N ) -> [].
> Unless you want to argue that 0 div N = 0 is an error? J
>
>
>
> V.
>
>
>  ------------------------------
>
> *From:* Robert Virding [mailto:]
> *Sent:* 05 December 2008 03:51 PM
> *To:* Valentin Micic
> *Cc:* Vlad Dumitrescu; erlang-questions
> *Subject:* Re: [erlang-questions] string:substr/2 gives confusing error
> message
>
>
>
> I think in cases like this you will find that the person who wrote the
> module (me most likely) was just trying to be kind and return something
> "sensible" if the arguments are of the right type and not too crazy. Like
> asking for more characters from a list than exists.
>
> I have now thought this over and come to the conclusion that you probably
> shouldn't be kind but generate an error in these situations. Being kind will
> often come back and bite you later in an unpleasant way.
>
> I personally don't see any problem with a function_clause error as these
> means that there was a bad argument. It could be helpful if the error came
> from the function which is called, but as long as it comes from a function
> which is cleary related to the call, as in this case, I don't worry about
> it.
>
> How to return errors is a difficult problem. If you catch them at a higher
> level how do you then return a helpful error? And helpful fo whom?
>
> Robert
>
> 2008/12/5 Valentin Micic <>
>
> I think that depends how you want to look at it...
> One may argue that
>        string:substr([], 2 )
> does not make much sense as a statement, but then, I'd say that:
>        string:substr("1", 2 )
> does not make to much sense either, and yet it returns [].
> IMO, both functions should behave the same way, and to be pragmatic, return
> an empty list (not a badarg exception).
>
> V.
>
>
>
> -----Original Message-----
> From: 
> [mailto:] On Behalf Of Vlad Dumitrescu
> Sent: 05 December 2008 12:12 PM
> To: erlang-questions
> Subject: [erlang-questions] string:substr/2 gives confusing error message
>
> Hi!
>
> Calling string:substr/2 with erroneous arguments gives a
> non-informative error message
>
> ()1> string:substr([], 2).
> ** exception error: no function clause matching string:substr2([],2)
>
> Shouldn't it be badarg instead, or something similar?
>
> Also, the strings module documentation doesn't mention what are the
> cases when the function will fail. (A quick glance shows that there
> are other functions in the same situation).
>
> best regards,
> Vlad
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20081205/7098466d/attachment.html>


More information about the erlang-questions mailing list