This post originated from an RSS feed registered with Java Buzz
by dion.
Original Post: Caller context and list.add(..).add(..).add(..)
Feed Title: techno.blog(Dion)
Feed URL: http://feeds.feedburner.com/dion
Feed Description: blogging about life the universe and everything tech
One of the first things that bugged me about Java when I first started to use it was a small thing. Why didn't add() return the list (or Vector back then) so I could chain the calls together.
I wanted to be able to:
Vector v = new Vector().add("foo").add("bar);
The same goes for having chained set* methods:
foo.setA(a).setB(b)
We see this used, especially in certain packages such as Hibernate.
One feature that I liked in Perl was the ability to know about the callers context:
my $scalar = @list; # or function which returns a list
is very different to:
my ($element) = @list; # or function which returns a list
Sometimes I would love to do the same in this world and say "If the caller of my set method wants the object back, give it, else we are in void world and that is fine too".
Of course, I don't think this would happen :)
Another way to make me a little more happy would be to give me named parameters, but that doesn't give me the same power.