{String,List}#[last]indexOf should return null instead of -1

172 views
Skip to first unread message

Sean Eagan

unread,
Sep 10, 2012, 11:40:01 AM9/10/12
to General Dart Discussion

In a pure OO language like Dart where int's are nullable, wouldn't it make more sense to have {String,List}#[last]indexOf return null instead of -1 ?

I realize it would be unfamiliar, but to me the benefits would outweigh the costs.  For example, it would integrate much more nicely with null safety operators if those are ever added.

Assuming Collection#contains is added, then most instances of explicit checks for -1:

if(list.indexOf(x) != -1) ...
if(str.indexOf(y) != -1) ...

will go away:

if(list.contains(x)) ...
if(str.contains(y)) ...

Wanted to run it by the list before filing a feature request.

Cheers,
Sean Eagan

Matthew Butler

unread,
Sep 10, 2012, 11:57:57 AM9/10/12
to mi...@dartlang.org
I'm personally in favour of this myself. Particularly coming from Ruby where array#index returns Nil (aka null), and where -1 is a valid index for an array. I realize it's not Dart's goal to be specifically familiar for the Ruby developer so much as javascript and Java developers, as long as the documentation is there and obvious, I don't think it would create a significant barrier. And to me, it's more reflective of the value I would expect to receive.

Matt

Josh Gargus

unread,
Sep 10, 2012, 2:52:06 PM9/10/12
to General Dart Discussion
I'm on the fence about your proposal. Returning null is a natural way
to indicate that no such index exists, and would play nicely with
null-safety operators, as you say. On the other hand, returning -1 is
well-known behavior in JavaScript and Java (and many others), and
given Dart's explicit focus on familiarity, I'm not sure that the
benefits are sufficient.

I'm definitely in favor of Collection#contains, but it is orthogonal
to whether indexOf() returns -1 or null.

Cheers,
Josh
> --
> Consider asking HOWTO questions at Stack Overflow:
> http://stackoverflow.com/tags/dart
>
>

Lasse Reichstein Holst Nielsen

unread,
Sep 10, 2012, 3:01:02 PM9/10/12
to mi...@dartlang.org
It would ruin the (occasionally useful) idiom:
  lastPart(string, separator) => string.substring(string.lastIndexOf(separator) + 1);

Bob Nystrom

unread,
Sep 10, 2012, 3:21:49 PM9/10/12
to mi...@dartlang.org
On Mon, Sep 10, 2012 at 12:01 PM, Lasse Reichstein Holst Nielsen <l...@google.com> wrote:
It would ruin the (occasionally useful) idiom:
  lastPart(string, separator) => string.substring(string.lastIndexOf(separator) + 1);

That idiom is useful, but feels pretty magical to me. Doing arithmetic intentionally including on a sentinel return value makes me feel dirty.

If this is common enough, maybe it should just be a method on string?

- bob

Christopher Wright

unread,
Sep 10, 2012, 4:21:44 PM9/10/12
to General Dart Discussion
In the D programming language, the convention is for find-index operations to return the length of the input on failure, so you can easily write things like:
firstPart(string, separator) => string.substring(0, string.indexOf(separator));

If you're talking about idioms, might as well mention that as an alternative, so we can argue about which use case would be more prevalent.


On 10 September 2012 12:01, Lasse Reichstein Holst Nielsen <l...@google.com> wrote:
It would ruin the (occasionally useful) idiom:
  lastPart(string, separator) => string.substring(string.lastIndexOf(separator) + 1);

--

Sean Eagan

unread,
Sep 10, 2012, 5:13:27 PM9/10/12
to mi...@dartlang.org
If there were a "default operator" (http://dartbug.com/1236) and indexOf returned null then you could do both pretty easily:

firstPart(string, separator) => string.substring(0, string.indexOf(separator) ?? string.length);
lastPart(string, separator) => string.substring((string.lastIndexOf(separator) ?? -1) + 1);

Cheers,
Sean

Sean Eagan

unread,
Sep 10, 2012, 5:17:38 PM9/10/12
to mi...@dartlang.org

I went ahead and filed a "star counter" for this at:
Reply all
Reply to author
Forward
0 new messages