Thinking some more and checking the code. Now substr/2 is defined as<br><br> substr(String, StartPos) -> String.<br><br>so substr returns the rest of string starting at StartPos. Logically this would mean that<br><br>
string:substr("a",2) -> []<br><br>is perfectly ok while<br><br> string:substr("",2)<br><br>should generate an error as there is no string starting at the second element in the string.<br><br>
Looking at substr/3 which is defined as <br><br> substr(String, StartPos, Length) -> String.<br><br>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.<br>
<br>But I do think that<br><br> string:substr("",2) -> []<br><br>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. :-)<br>
<br>Robert<br><br><div class="gmail_quote">2008/12/5 Valentin Micic <span dir="ltr"><<a href="mailto:v@pharos-avantgard.com">v@pharos-avantgard.com</a>></span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div link="blue" vlink="blue" lang="EN-US">
<div><div class="Ih2E3d">
<p><font color="navy" size="2" face="Arial"><span style="font-size: 10pt; font-family: Arial; color: navy;">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? </span></font><font color="navy" size="2" face="Wingdings"><span style="font-size: 10pt; font-family: Wingdings; color: navy;">J</span></font><font color="navy" size="2" face="Arial"><span style="font-size: 10pt; font-family: Arial; color: navy;"></span></font></p>
<p><font color="navy" size="2" face="Arial"><span style="font-size: 10pt; font-family: Arial; color: navy;"> </span></font></p>
<p><font color="navy" size="2" face="Arial"><span style="font-size: 10pt; font-family: Arial; color: navy;">V.</span></font></p>
<p><font color="navy" size="2" face="Arial"><span style="font-size: 10pt; font-family: Arial; color: navy;"> </span></font></p>
</div><div>
<div style="text-align: center;" align="center"><font size="3" face="Times New Roman"><span style="font-size: 12pt;">
<hr align="center" size="2" width="100%">
</span></font></div>
<p><b><font size="2" face="Tahoma"><span style="font-size: 10pt; font-family: Tahoma; font-weight: bold;">From:</span></font></b><font size="2" face="Tahoma"><span style="font-size: 10pt; font-family: Tahoma;"> Robert Virding
[mailto:<a href="mailto:rvirding@gmail.com" target="_blank">rvirding@gmail.com</a>] <br><div class="Ih2E3d">
<b><span style="font-weight: bold;">Sent:</span></b> 05 December 2008 03:51 PM<br>
<b><span style="font-weight: bold;">To:</span></b> Valentin Micic<br>
<b><span style="font-weight: bold;">Cc:</span></b> Vlad Dumitrescu;
erlang-questions<br>
</div><b><span style="font-weight: bold;">Subject:</span></b> Re: [erlang-questions]
string:substr/2 gives confusing error message</span></font></p>
</div><div><div></div><div class="Wj3C7c">
<p><font size="3" face="Times New Roman"><span style="font-size: 12pt;"> </span></font></p>
<p style="margin-bottom: 12pt;"><font size="3" face="Times New Roman"><span style="font-size: 12pt;">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.<br>
<br>
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.<br>
<br>
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.<br>
<br>
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?<br>
<br>
Robert</span></font></p>
<div>
<p><font size="3" face="Times New Roman"><span style="font-size: 12pt;">2008/12/5 Valentin Micic <<a href="mailto:v@pharos-avantgard.com" target="_blank">v@pharos-avantgard.com</a>></span></font></p>
<p><font size="3" face="Times New Roman"><span style="font-size: 12pt;">I think that depends how you want to look at it...<br>
One may argue that<br>
string:substr([], 2 )<br>
does not make much sense as a statement, but then, I'd say that:<br>
string:substr("1", 2 )<br>
does not make to much sense either, and yet it returns [].<br>
IMO, both functions should behave the same way, and to be pragmatic, return<br>
an empty list (not a badarg exception).<br>
<font color="#888888"><span style="color: rgb(136, 136, 136);"><br>
V.</span></font></span></font></p>
<div>
<div>
<p><font size="3" face="Times New Roman"><span style="font-size: 12pt;"><br>
<br>
-----Original Message-----<br>
From: <a href="mailto:erlang-questions-bounces@erlang.org" target="_blank">erlang-questions-bounces@erlang.org</a><br>
[mailto:<a href="mailto:erlang-questions-bounces@erlang.org" target="_blank">erlang-questions-bounces@erlang.org</a>]
On Behalf Of Vlad Dumitrescu<br>
Sent: 05 December 2008 12:12 PM<br>
To: erlang-questions<br>
Subject: [erlang-questions] string:substr/2 gives confusing error message<br>
<br>
Hi!<br>
<br>
Calling string:substr/2 with erroneous arguments gives a<br>
non-informative error message<br>
<br>
(w@EV001B784AC0A2)1> string:substr([], 2).<br>
** exception error: no function clause matching string:substr2([],2)<br>
<br>
Shouldn't it be badarg instead, or something similar?<br>
<br>
Also, the strings module documentation doesn't mention what are the<br>
cases when the function will fail. (A quick glance shows that there<br>
are other functions in the same situation).<br>
<br>
best regards,<br>
Vlad<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a></span></font></p>
</div>
</div>
</div>
<p><font size="3" face="Times New Roman"><span style="font-size: 12pt;"> </span></font></p>
</div></div></div>
</div>
<br>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br></blockquote></div><br>