On Mon, Feb 09, 2009 at 05:27:42AM -0800, Reinier Zwitserloot wrote:
> 1. By using a constructor, you don't get type inference. Use a static
> method, like so:
>
> public static <P, Q> Pair<P, Q> create(P a, P b) {
> return new Pair<P, Q>(a, b);
> }
>
> and then make the actual constructor private.
Hear, hear! Although, I'd call it "of" rather than create, as that's
what the google collections library does. The type inference leads to a
really nice API.
public Pair<String, Integer> doStuff() {
// …
return Pair.of("answer", 42);
}
As to the issues of getters / setters:
* Don't have setters, pairs should be immutable.
* Do you really need to get at pairs from within JavaBeans? I'd guess
probably not. Most of the time, the components of a pair will be
extracted within a line or two of the call site.
A nice discussion, anyways. I wish my iPod hadn't run out of juice half
way through. :)
-Dom
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "The
Java Posse" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaposse?hl=en
-~----------~----~----~----~------~----~------~--~---