unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Tassilo Horn <tassilo@member.fsf.org>
To: Bastien <bzg@gnu.org>
Cc: Juanma Barranquero <lekktu@gmail.com>,
	Stefan Monnier <monnier@IRO.UMontreal.CA>,
	emacs-devel@gnu.org
Subject: Re: Why (substring "abc" 0 4) does not return "abc" instead of an error?
Date: Mon, 16 Jul 2012 19:57:15 +0200	[thread overview]
Message-ID: <874np7h9ms.fsf@thinkpad.tsdh.de> (raw)
In-Reply-To: <87hat7bqmh.fsf@gnu.org> (Bastien's message of "Mon, 16 Jul 2012 18:46:46 +0200")

Bastien <bzg@gnu.org> 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
indices.

Bye,
Tassilo



  reply	other threads:[~2012-07-16 17:57 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-15 23:15 Why (substring "abc" 0 4) does not return "abc" instead of an error? Bastien
2012-07-15 23:29 ` Juanma Barranquero
2012-07-15 23:59   ` Bastien
2012-07-16  0:10     ` Juanma Barranquero
2012-07-16  7:14       ` Bastien
2012-07-16 16:15         ` Stefan Monnier
2012-07-16 16:22           ` Bastien
2012-07-16 16:46           ` Bastien
2012-07-16 17:57             ` Tassilo Horn [this message]
2012-07-16 18:51               ` Lars Magne Ingebrigtsen
2012-07-16 19:30                 ` Bastien
2012-07-16 19:30                 ` Tassilo Horn
2012-07-16 20:20                 ` Pascal J. Bourguignon
2012-07-16 19:25               ` Bastien
2012-07-16 19:43                 ` Bastien
2012-07-16 20:19             ` Pascal J. Bourguignon
2012-07-16 20:30             ` Stefan Monnier
2012-07-16 22:28               ` Lennart Borgman
2012-07-16 22:48                 ` Bastien
2012-07-16 22:53                   ` Lennart Borgman
2012-07-16  7:38       ` Andreas Schwab
2012-07-16  9:40         ` Juanma Barranquero
  -- strict thread matches above, loose matches on Subject: below --
2012-07-16  3:45 Dmitry Gutov
2012-07-16  7:32 ` Bastien
2012-07-16  7:52   ` Thierry Volpiatto
2012-07-16  8:38     ` Bastien
2012-07-16 13:03   ` Dmitry Gutov
2012-07-16 14:32     ` Bastien
2012-07-16 13:10 ` Pascal J. Bourguignon
2012-07-16 14:40   ` Bastien
2012-07-16 15:00     ` Pascal J. Bourguignon
2012-07-16 15:07       ` Lennart Borgman
2012-07-16 15:19         ` Pascal J. Bourguignon
2012-07-16 15:22         ` Bastien
2012-07-16 15:46       ` Bastien
2012-07-16 15:49         ` Bastien
2012-07-16 19:49           ` Thien-Thi Nguyen
2012-07-16 22:32             ` Bastien
2012-07-16 15:56         ` Pascal J. Bourguignon
2012-07-16 16:13           ` Bastien
2012-07-16 19:00 Dmitry Gutov
2012-07-16 19:51 ` Tassilo Horn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=874np7h9ms.fsf@thinkpad.tsdh.de \
    --to=tassilo@member.fsf.org \
    --cc=bzg@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=lekktu@gmail.com \
    --cc=monnier@IRO.UMontreal.CA \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).