Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!postnews.google.com!news3.google.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Nigel Wade Newsgroups: comp.lang.java.programmer Subject: Re: Call by Result Date: Fri, 10 Jun 2011 10:03:26 +0100 Lines: 35 Message-ID: <95e4uuF3cvU1@mid.individual.net> References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: individual.net IsQFs/Xrx7fv/TJN1cqIyAuumIlmtJG2D5/FIhvykoL41e1sj9 Cancel-Lock: sha1:uWsgtlqUgHllwqM1zmW6V/QYZWg= User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-GB; rv:1.9.1.16) Gecko/20101125 SUSE/3.0.11 Thunderbird/3.0.11 In-Reply-To: Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:5172 On 10/06/11 08:33, Andreas Leitgeb wrote: > Gene Wirchenko wrote: >> I am writing a simple preprocessor. I have a few spots where a >> string needs to be parsed. I want to call something like this: >> String ReturnString=""; >> boolean DidItWork=GetString(ReturnString); >> if (!DidItWork) >> // too bad >> It is not acceptable to have a special String value mean failure. I >> want the method to be able to return any potential string. > > There's three basic ways to do it: > > 1) Return null to indicate failure. > > 2) instead of the string, pass a mutable container of a string: > > 3) encode the String: e.g.: prepend a particular text to "success"- Are 1) and 3) not precluded by the proviso "It is not acceptable to have a special String value mean failure". A fourth way would be to return a class/object containing both the boolean and the String. To me, this would be the natural OO way of returning more information than a primitive type. And a 5th way could be to return the String and throw an Exception if it did not work. Some purists may argue that failure to work is not strictly an exception, but if it gets the job done... -- Nigel Wade