[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why (substring "abc" 0 4) does not return "abc" instead of an error?

From: Tassilo Horn
Subject: Re: Why (substring "abc" 0 4) does not return "abc" instead of an error?
Date: Mon, 16 Jul 2012 19:57:15 +0200
User-agent: Gnus/5.130006 (Ma Gnus v0.6) Emacs/24.1.50 (gnu/linux)

Bastien <address@hidden> writes:

>> We have general functionality when you want to ignore some errors,
>> such as condition-case.
> Also, I'm fine with
>   (substring "abc" -1 1)
>     => #ERROR

I don't see why that justifies an error and (substring "abc" 0 4) does
not.  -1 is a valid FROM index meaning the length of the string minus
one.  Its just that the TO index is smaller than FROM here, but IMO
that's the same class of errors as a too large TO index.

> so using ̀€condition-case' would not help me distinguish
> between the case above and (substring "abc" 0 4), which
> is what I want.

A condition-case handler has access to the args given to the erroring
form, so the cases could be distinguished although both signal
args-of-range.  Well, not that it would help you much here.  

> I see the benefit of having 
>   (substring "abc" 0 4)
>     => "abc"
> in terms of simplifying Elisp writing -- and I still fail
> to see the harm (but maybe Pascal will tell me where he has
> been bitten by this.)

In my experiences, out-of-range indices into strings or arrays are
almost always programming errors.  I'm not even able to come up with
some concrete use-case where I'd like to have the suggested behavior.
Either I know exactly what I'm operating on and use indices, or I have
only some general assumptions and then use more fuzzy things like
splitting by regular expressions.

BTW: C++ string's substr method doesn't quite have the suggested
behavior.  It's arguments are not two indices, but one index and one
length n (the number of characters that should be returned).  If the
index is out of range, you'll get an out_of_range exception.  n however
may span longer than the rest of the string in which case the returned
string is shorter than the given length n.  But that's a different
story: indexes have to be in range.

Ditto for Ruby: String::slice also gets an index and a length, not two


reply via email to

[Prev in Thread] Current Thread [Next in Thread]