Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Call by Result

Reply
Thread Tools

Call by Result

 
 
Gene Wirchenko
Guest
Posts: n/a
 
      06-10-2011
On 10 Jun 2011 13:47:27 GMT, http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Stefan Ram)
wrote:

>Gene Wirchenko <(E-Mail Removed)> writes:
>>I wish to call by result with a method.


[snip]

>http://en.wikipedia.org/wiki/Evaluation_strategy
>
> Did you all knew this before you answered? The reference
> has to be /uninitialized/ for an evaluation strategy to be
> called "call by result"!


The various call types are not exotic. If you do not know COLD
the terms call-by-reference, call-by-value, call-by-result, and
call-by-value-result (also known as copy-restore), you have a bit of
studying to do. Well, call-by-name is somewhat exotic, but it is
thought to have been a bit of an accident.

>>String ReturnString="";
>>boolean DidItWork=GetString(ReturnString);

>
> But in Java we cannot pass uninitialized references!


A weakness. My UNfavourite Java compilation error message is
that the variable might not have been initialised in code where it
does not matter.

> Especially, in this case, the reference is not uninitialized.


I have to do that to kowtow to Java.

[snip]

Sincerely,

Gene Wirchenko
 
Reply With Quote
 
 
 
 
Gene Wirchenko
Guest
Posts: n/a
 
      06-10-2011
On Fri, 10 Jun 2011 07:22:20 -0700, Peter Duniho
<(E-Mail Removed)> wrote:

>On 6/10/11 6:47 AM, Stefan Ram wrote:
>> [...]
>> But I was able to look it up in an online encyclopedia:
>>
>> »When the reference is passed to the callee
>> uninitialized, this evaluation strategy may
>> be called "call by result".«
>>
>> http://en.wikipedia.org/wiki/Evaluation_strategy
>>
>> Did you all knew this before you answered? The reference
>> has to be /uninitialized/ for an evaluation strategy to be
>> called "call by result"!

>
>Maybe that's true technically. But the OP's code initializes the
>variable before calling the method. I think it's just as likely that
>he's not using the term precisely as defined in your reference, and that
>all of the answers do get to the heart of what he's _really_ asking.


Java requires initialisation.

>That is, it seems more likely that what the OP cares about is the
>"modify the argument, return a boolean" aspect more than he cares about
>the "was the argument initialized" aspect.


Yes.

Sincerely,

Gene Wirchenko
 
Reply With Quote
 
 
 
 
Gene Wirchenko
Guest
Posts: n/a
 
      06-10-2011
On 10 Jun 2011 14:37:09 GMT, (E-Mail Removed)-berlin.de (Stefan Ram)
wrote:

>Peter Duniho <(E-Mail Removed)> writes:
>>That is, it seems more likely that what the OP cares about is
>>the "modify the argument, return a boolean" aspect more than
>>he cares about the "was the argument initialized" aspect.

>
> Ok, the OP contained:
>
>>>>String ReturnString="";
>>>>boolean DidItWork=GetString(ReturnString);
>>>>if (!DidItWork)

>
> For my taste, the object-oriented way to do this is:
>
>final Getter getter = new Getter();
>getter.get();
>if( getter.isValid() )result = getter.value(); else ...
>
> , while the Java way to do this is
>
>try{ s = getString(); }
>catch( final Exception exception )...


I care about getting the job done. I use OOP when it is
appropriate and do not use it when it is not appropriate.

> . The OP used upper-case names of variables and methods,
> so he might be a beginner in Java, in which case it might be
> more appropriate for him to learn how things are natively
> done in Java than to learn how to emulate features of other
> languages (such as call by result) in Java.


I am also someone who has been programming professionally for
about twenty-five years. In that time, I have worked out conventions
across different languages for variable-naming.

Sincerely,

Gene Wirchenko
 
Reply With Quote
 
Gene Wirchenko
Guest
Posts: n/a
 
      06-10-2011
On Fri, 10 Jun 2011 17:01:36 +0100, RedGrittyBrick
<(E-Mail Removed)> wrote:

>On 10/06/2011 07:03, Gene Wirchenko wrote:


[snip]

>> 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 think Stephan Ram's answer is roughly what I would do.
>
>String returnString;
>Thagomizor thangomizor = new Thagomizor();
>thangomizor.thangomize();
>if (!thangomizor.didItWork()) {
> throw new TooBadException();
>} else {
> returnString = thangomizor.getString();
>}


Lengthy code (compared with what it could be) to no additional
benefit.

Sincerely,

Gene Wirchenko
 
Reply With Quote
 
Steven Simpson
Guest
Posts: n/a
 
      06-11-2011
On 10/06/11 07:03, 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


I don't think the following suggestion has been covered exactly. Split
GetString() into two methods: the first testing for the string, storing
it in the containing class, and returning true or false; the second
simply retrieving the last string.

I'm assuming that GetString is non-static for this.

class Parser {
/**
* Attempt to parse a string. If successful, the result
* is obtained from {@link #getLastString()}.
*
* @return true if a string was parsed
*/
public boolean parseString() { ... }
public String getLastString() { ... }
}

if (parser.parseString()) {
doSomethingWith(parser.getLastString());
}

 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      06-11-2011
Gene Wirchenko <(E-Mail Removed)> wrote:
> On 10 Jun 2011 07:33:12 GMT, Andreas Leitgeb
><(E-Mail Removed)> wrote:
>>2) instead of the string, pass a mutable container of a string:
>> String[] stringContainer = new String[1];
>> boolean didItWork = getString(stringContainer);
>> if (didItWork) { /* stringContainer[0] has the string */ }

>
> This is a kludge but fairly simple. I did a bit more looking and found
> http://java.sun.com/docs/books/jls/s...alues.doc.html
> where a simple class is created. I ended up with


I think "kludge" is not the apt term for mine. "hack" may be a better.
As you pointed out already, it is equivalent to creating such a container-
class, but there's just the tradeoff:
- accept some ugliness with "[0]"
- add yet another trivial class to your project or to
your library's whole interface.
Maybe I'm a bad Java-programmer, myself, because I'd go for first,
unless there is some domain-related meaning to such a container
class, like e.g. "FooState" which would then likely contain more
than just one field.

>>PS: Surely, someone will soon point out coding-conventions about
>> upper-/lower-casing different kinds of identifiers. I dare to agree
>> in advance. (Btw., I changed to conformant casing in my examples.)

>
> I have used a number of languages. One of the things that I
> dislike about Java is the small letter first style. It is
> particularly bothersome, because my variable naming convention often
> has HN-like prefixes.


So, actually the method here would be named sGetString(), that's fine
with Java's *case*-conventions (but perhaps not with others...)

 
Reply With Quote
 
Abu Yahya
Guest
Posts: n/a
 
      06-11-2011
On 6/11/2011 8:37 PM, Andreas Leitgeb wrote>
> So, actually the method here would be named sGetString(), that's fine
> with Java's *case*-conventions (but perhaps not with others...)
>


What does the initial "s" signify here?
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      06-11-2011
(E-Mail Removed)-berlin.de (Stefan Ram) writes:
>For my taste, the object-oriented way to do this is:
>final Getter getter = new Getter();
>getter.get();
>if( getter.isValid() )result = getter.value(); else ...


Trehinking about this again, the object-oriented way to do
this is now (»lambda« and »java.lang.Report« are not part of
Java 6, but hypothetical):

new StringGetter().get
( lambda( final java.lang.String string ){ ... },
lambda( final java.lang.Report report ){ ... });

To understand this, one has to remember that in an
object-oriented language, blocks of code also are objects
(»everything is an object«). »get« would be defined so as to
call its first argument with the value read upon success, or
otherwise call its second argument with an error report upon
failure.

Without static types and with some hypothetical syntax
this becomes:

new StringGetter().get( [string|...], [report|...] );

This is just the realization of »Tell, Don't Ask«.
Here, one asks the object:

if( getter.isValid() )...

. Here, one tells the object what to do:

new StringGetter().get( [string|...], [report|...] );

 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      06-11-2011
Martin Gregorie <(E-Mail Removed)> wrote:
> I think its because GW is using a variant of the so-called Hungarian
> Notation which AFAIK is a Microsoft invention, initially applied to C and
> C++.
> The idea is to make the name of a function or method document the type of
> object it returns by prefixing the name with an alphabet soup of
> initials, so a function returning a string might be called
> sMyFunction(). The catch with it is that, if you need to change the type
> of the returned value, you need to change all references to it as well,


I wouldn't exactly call that a "catch". If you change the return type
of a method, you'd f...ing better have a look at all call-sites
At least, the compiler will help you, if it finds old sGetString() still
referenced somewhere. Without changing the name along with the return-
type, you *might* not notice all calls, and end up with a runtime-crash.

There's an entirely different catch, however, namely that people might
change the type but *not* the name, and you end up with the same trouble
as without HN, except wrong expectations on seeing sGetString() which
maybe has been changed to return an Integer.

> e.g if the function should rather return a pointer to the string you have
> to rename it to psMyFunction() and then hunt through the source file(s)
> changing all references to it as well.

 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      06-11-2011
Stefan Ram <(E-Mail Removed)-berlin.de> wrote:
> This is just the realization of »Tell, Don't Ask«.
> Here, one asks the object:
> if( getter.isValid() )...
> Here, one tells the object what to do:
> new StringGetter().get( [string|...], [report|...] );


If you'd like coding like that, you *might* like Tcl.

Passing scriptlets to commands to have them executed (conditionally,
repeatedly, deferredly, just later, within a different context, ...)
is just simply normal in Tcl. Even the "if" statement itself is
merely a command that gets passed scriptlets and executes one (or maybe
even none - if no condition is true and no "else"-scriptlet given) ...

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
i = 10; result = ++i - --i; How result become ZERO Lakshmi Sreekanth C Programming 52 09-23-2010 07:41 AM
Re: i = 10; result = ++i - --i; How result become ZERO Mr. Buffoon C Programming 4 09-23-2010 03:01 AM
Is the result of valid dynamic cast always equal to the result ofcorrespondent static cast? Pavel C++ 7 09-18-2010 11:35 PM
simulation result is correct but synthesis result is not correct J.Ram VHDL 7 12-03-2008 01:26 PM
1. Ruby result: 101 seconds , 2. Java result:9.8 seconds, 3. Perl result:62 seconds Michael Tan Ruby 32 07-21-2005 03:23 PM



Advertisments