Fluent methods often allows for much more compact and readable syntax.
But it's not without problems:
- fluent setters are incompatible with java.beans.Introspector
- fluent setters often use different naming convention
- most of available Java methods are not fluent
- mixing fluent and classical methods may be ugly and confusing
I'd love Java with things like
map.put(key1, value1).put(key2, value2);
but this will simply never happen - at least not for java.util.Map.
...unless we change the syntax. AFAIK, Reinier plans already some Java
syntax extension. I also think he finds the fluentness quite
important.
I've got different (but similar) ideas for the syntax, I hope, not all
of them are stupid:
Version 0: Allow statements like above and treat all method returning
void as if they'd return their this.
This could be a bit confusing and wouldn't work e.g. with Map.put as
it returns a result (although it gets ignored most of the time).
Version 1: Allow statements starting with a dot followed by an
identifier like
map.put(key1, value1);
.put(key2, value2);
The semantic should be equivalent to a program with the last top-level
object repeated before the dot. Let me make it clear by examples:
ListOfMaps.get(0).put(key1, (inputStream.readLine()));
.put(key2, value2)
is equivalent to
Map _map = ListOfMaps.get(0); // _map is a new local variable
_map.put(key1, (inputStream.readLine()));
_map.put(key2, value2); // not inputStream since it was not at the top
level
There may be some strange cases like
if (cond) map.put(key1, value1);
.put(key2, value2);
Any such case should simply lead to a compile error.
Version 2: This version looks about the same, but uses ";." as an
operator (no spaces between the two chars are allowed), so you must
write
map.put(key1, value1);.put(key2, value2);
I don't like it since the semicolon in my mind always terminates a
statement.
Version 3: The same like above, but with "&." as the operator:
map.put(key1, value1) &.put(key2, value2);
This could be quite readable and intuitive. The statement
if (cond) map.put(key1, value1) &.put(key2, value2);
would get an obvious meaning then.
--
You received this message because you are subscribed to the Google
Groups group for
http://projectlombok.org/
To post to this group, send email to
project...@googlegroups.com
To unsubscribe from this group, send email to
project-lombo...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/project-lombok?hl=en