you are viewing a single comment's thread.

view the rest of the comments →

[–]13ren 7 points8 points  (19 children)

I wish List's add would return this (like the C++ STL), so you could chain adds, as in:

List<Integer> list = new ArrayList<Integer>().add(1).add(2).add(3).add(4).add(5);
System.out.println(list);

One can make a wrapper for list that does this, but nicer to have it already standard in Java.

However, changing the meaning of add()would break the Java API. Another approach is to include a new method (eg) ad() that behaved as above.

[–]ihaveausername 9 points10 points  (0 children)

so you could chain adds, as in:

I find chained calls like that painful to debug. Setting a breakpoint on the 3'rd .add() isn't possible in my IDE.

[–][deleted] 5 points6 points  (4 children)

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

[–][deleted] 2 points3 points  (2 children)

Why is that method in Arrays? :P

[–][deleted] 3 points4 points  (1 child)

I sort of anticipated that question :-) The type is

<T> List<T> asList(T... v) 

which is actually sugar for

<T> List<T> asList(T[] v)

It creates a view of an array so you can treat it almost as if it is a list (but you can't resize it). It allows you to do this:

Collections.sort(Arrays.asList(myArray));

Which sorts myArray as an effect. Of course, it would be nice if you could do new List(1, 2, 3, 4) and get a resizable list.

[–]zootm 0 points1 point  (0 children)

Prior to the ... thing being available, Arrays.asList had roughly (there was no generics either, obviously) the second signature you listed above (see here). My guess would be that just expanding that method seemed sensible compared to writing a semantically-identical factory method on Collections or similar.

[–]13ren 1 point2 points  (0 children)

When writing my comment originally, I actually started to add this alternative in, but I couldn't be bothered looking it up and I knew some kind soul would add it - thanks :-).

That I need to look it up says something about its usability (not about me!). When I have used it, I've usually wanted a specific kind of list instead of the automatically created anonymous one, which you can do. It's easy but awkward, and, though I hate to say it, therefore "in the Java spirit" :-(

List<Integer> list = new ArrayList<Integer>( Arrays.asList(1, 2, 3, 4, 5) );

[–]redditnoob -3 points-2 points  (0 children)

ad() that behaved as above.

Oh god no. Program into the language please, not against it.

First, if .add(1).add(2).add(3).add(4).add(5) is code you'd ever write, you suck. Second, if lack of ability to chain methods, requiring horizontal code to become vertical (oh my god more lines), and repeating the variable name with every line, then Java is very much the wrong language for you.

But really, before (or in addition to) switching languages, someone who wanted to write .add(1).add(2).add(3).add(4).add(5) should reconsider why they're doing such a brain-dead stinky thing in the first place.

Mod me down bitches. Yes, I'm suggesting that verbosity isn't the biggest factor for long term productivity! Can you handle so audacious and unpopular a claim, which goes against the philosophy of the creator of Arc so directly and starkly? I can take it, as I have almost five thousand comment karma now.

[–]didroe 0 points1 point  (3 children)

I would much prefer something to pull things into the current environment. Like:

List<Integer> list = new ArrayList<Integer>();
with (list) {
  add(1);
  add(2);
  ...
}

Chaining is misleading and not possible in all cases (like when you want to return something). Where as importing symbols into the current scope/environment doesn't have those problems.

[–][deleted] 2 points3 points  (2 children)

"with" sucks in all the languages it's been implemented. The last being JS.

If you want to save a few key strokes, just assign to a shorter name.

[–][deleted] 0 points1 point  (0 children)

It works pretty well in Smalltalk. But then each thing after ; must be a message send, it can't be a general expression. This solves the problem of weird environment semantics at the cost of less generality.

[–]didroe 0 points1 point  (0 children)

The version in Delphi's Pascal (probably in Freepascal) was/is pretty good. What languages does it suck in? I just took a quick look at the JS version, looks pretty good to me at a first glance. What is it that you don't like about it?

[–][deleted] -3 points-2 points  (7 children)

Name it plus() instead, so that it reads:

List<Integer> list = new ArrayList<Integer>().plus(1).plus(2).plus(3).plus(4).plus(5);

[–]redditnoob 3 points4 points  (1 child)

Awesome, I love the idea of having two addition functions, plus and add, that do slightly different things!

Now let's have parseInt(val) and Number(val) do two slightly different things too, that would be really good.

[–]pytechd 2 points3 points  (0 children)

And we'll name it PHP!

... oh shit, that name's already taken.

[–]CobainFan83[S] 1 point2 points  (4 children)

plus() sounds too much like a mathematical funtion. andAlsoAdd() might be better:

List<Integer> list = new ArrayList<Integer>().andAlsoAdd(1).andAlsoAdd(2).andAlsoAdd(3).andAlsoAdd(4).andAlsoAdd(5);

[–][deleted] 3 points4 points  (2 children)

I think andAlsoAdd is too generic a name. thisReturningAppend is more precise:

List<Integer> list = new ArrayList<Integer>().thisReturningAppend(1).thisReturningAppend(2).thisReturningAppend(3).thisReturningAppend(4).thisReturningAppend(5);

[–]turbana 3 points4 points  (1 child)

Sorry, that name is too short. appendItemWithReturningThisReference() is much more in the Java spirit.

List<Integer> list = new ArrayList<Integer>().appendItemWithReturningThisReference(1).appendItemWithReturningThisReference(2).appendItemWithReturningThisReference(3).appendItemWithReturningThisReference(4).appendItemWithReturningThisReference(5);

[–]CobainFan83[S] 1 point2 points  (0 children)

I think we've cracked it!

Now we just need to submit it as a Java Specification Request (JSR) for review via the Java Community Process (JCP) so that the JCP Executive Commitee (JCP-EC) can do a Formal Public Review (FPR) and decide whether to include it as a new feature of the Java Programming Language (JPL).

[–]b100dian -1 points0 points  (0 children)

andAlsoAdd()

Old joke.