Although I certainly agree in principle that it is vastly preferable to work with immutable objects where possible, in this case I don't see a strong need to change Reference or even to add an ImmutableReference type, since it's easy to convert to and from an existing immutable type, java.net.URI. The class comment does say, right at the top: "Contrary to the java.net.URI class, this interface represents mutable references." That's pretty clear, although I think it could be worded better, e.g., "Unlike java.net.URI, instances of this class are mutable."
It's better to treat Reference as something like a Builder<URI>. If you're trying to stay functional in spirit, use toUri() as early as possible, store everything under your control as java.net.URI, and only convert back to Reference to pass things back to the Restlet framework (or to take advantage of Reference methods that URI doesn't support). --tim On Sun, Feb 8, 2009 at 2:45 PM, Malcolm Sparks <[email protected]> wrote: > Hi, > > I've been using the restlet framework for a while now, and it's a > really well-written system, I really like it. > > However, I got a nasty surprise the other day when I tried to do > something like this: > > 1 Reference ref = new Reference("/foo"); > 2 doSomethingWithRef(ref); > 3 doSomethingElseWithRef(ref); > > I got strange behavior on line 3, because line 2 mutated the reference > created in line 1, so it was no longer '/foo'. > > This was a surprise to me, because I've been working with functional > languages quite a lot recently and they just don't behave this way, > and avoid a lot of nasty side-effects as a result. > > I agree that it's a nice design to use a reference to derive another > reference, eg. > > Reference ref = new Reference("/foo"); > Reference ref2 = ref.addQueryPath("bar=zip"); > > But it would be really cool if Reference instances were immutable > because then you could safely re-use them, and it would be easier to > avoid bugs when writing restful apps. > In example above, adding a query path shouldn't change the original > reference. > > Thanks, > > Malcolm > > ------------------------------------------------------ > > http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=1125259 > ------------------------------------------------------ http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=1125452

