Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: markspace <-@.> Newsgroups: comp.lang.java.programmer Subject: Re: Call by Result Date: Fri, 10 Jun 2011 08:43:41 -0700 Organization: A noiseless patient Spider Lines: 37 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 10 Jun 2011 15:43:42 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="9mIMLLWQE/uBQz+Vsit8fg"; logging-data="6420"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+j0QRYTfoOEUPCdMzi/bVvC1ySk+Vm5As=" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10 In-Reply-To: Cancel-Lock: sha1:TQt4MA57hCgDCZOtYCCnwohpIgQ= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:5185 On 6/9/2011 11:03 PM, Gene Wirchenko wrote: > Dear Java'ers: > > I wish to call by result with a method. Is it possible? If not, > can it be easily simulated in an unnasty way? > > 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. I'm kind of surprised by all the answers here also. I'm glad Stefan pointed out the definition of "call by result," which I also did not know. However call-by-result is just a use case of pass-by-reference, so why not use that? In Java the standard quicky semantic is to use an array. String[] returnString = new String[1]; // just 1 return string boolean didItWork = GetString( returnString ); if( didItWork ) { String result = returnString[0]; .... process result here } else { .... too bad } returnString is just a holder, as was mentioned earlier, but temporary arrays are often used for pass by reference in Java (which only has pass by value), so any programmer is likely to grasp what is going on here quickly.