Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Call by Result

 
 
Paul Cager
Guest
Posts: n/a
 
      06-17-2011
On Jun 16, 3:30*pm, Cholo Lennon <(E-Mail Removed)> wrote:
> IMHO using exceptions is not always the best answer to this problem (as
> other suggested).
>


Not always, no. But in this case there are hints that the OP regarded
the lack of a String as an error condition - he used phrases such as
"mean failure" and "DidItWork". In that case I think an exception is
appropriate (and less work than many of the other suggestions!).

I believe that cases where neither of the following two options are
appropriate are relatively rare:

- returning null, meaning the "thing" was not found, but that's
only to be expected.
- throwing an exception, meaning that you can't return anything
because an error happened.
 
Reply With Quote
 
 
 
 
lewbloch
Guest
Posts: n/a
 
      06-17-2011
On Jun 16, 7:30*am, Cholo Lennon <(E-Mail Removed)> wrote:
> On 10/06/2011 03:03, 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.

>
> > Sincerely,

>
> > Gene Wirchenko

>
> IMHO using exceptions is not always the best answer to this problem (as
> other suggested).
>
> How about using a simple generic class? (of course, it could be
> corrected or improved. A similar idea could be found in other languages
> (such as C++ (boost)):
>
> class Optional<T> {
> * * *private T value;
> * * *private boolean valid;
>
> * * *public Optional(T value) {
> * * * * *setValue(value);


It's dangerous to call an overridable method from a constructor.

> * * *}
>
> * * *public Optional() {
> * * * * *this.valid = false;


Redundant assignment, but arguably it adds clarity. I don't think it
does, but one could argue that it does.

> * * *}
>
> * * *public boolean isValid() {
> * * * * *return this.valid;
> * * *}
>
> * * *public T getValue() {
> * * * * *return this.value;
> * * *}
>
> * * *public void setValue(T value) {
> * * * * *this.value = value;
> * * * * *// TODO Shall I set to true when value is null?
> * * * * *this.valid = true;
> * * *}
>
> * * *public void invalidate() {
> * * * * *this.value = null;
> * * * * *this.valid = false;
> * * *}
>
> }
>


The original post establishes 'null' as an out-of-band value. Your
code hints at that possibility in a comment, but doesn't enforce the
requirement. If 'null' is out of band, then the boolean variable is
redundant and duplicative. If 'null' is in band, then the boolean
makes more sense, as it would have to to justify the complexity.

--
Lew
 
Reply With Quote
 
 
 
 
Cholo Lennon
Guest
Posts: n/a
 
      06-17-2011
On 17/06/2011 10:40, lewbloch wrote:
> On Jun 16, 7:30 am, Cholo Lennon<(E-Mail Removed)> wrote:
>> On 10/06/2011 03:03, 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.

>>
>>> Sincerely,

>>
>>> Gene Wirchenko

>>
>> IMHO using exceptions is not always the best answer to this problem (as
>> other suggested).
>>
>> How about using a simple generic class? (of course, it could be
>> corrected or improved. A similar idea could be found in other languages
>> (such as C++ (boost)):
>>
>> class Optional<T> {
>> private T value;
>> private boolean valid;
>>
>> public Optional(T value) {
>> setValue(value);

>
> It's dangerous to call an overridable method from a constructor.


Agree, you're right. It was for simplicity.

>
>> }
>>
>> public Optional() {
>> this.valid = false;

>
> Redundant assignment, but arguably it adds clarity. I don't think it
> does, but one could argue that it does.
>


Agree, you're right, it was written for clarity.

>> }
>>
>> public boolean isValid() {
>> return this.valid;
>> }
>>
>> public T getValue() {
>> return this.value;
>> }
>>
>> public void setValue(T value) {
>> this.value = value;
>> // TODO Shall I set to true when value is null?
>> this.valid = true;
>> }
>>
>> public void invalidate() {
>> this.value = null;
>> this.valid = false;
>> }
>>
>> }
>>

>
> The original post establishes 'null' as an out-of-band value. Your
> code hints at that possibility in a comment, but doesn't enforce the
> requirement. If 'null' is out of band, then the boolean variable is
> redundant and duplicative. If 'null' is in band, then the boolean
> makes more sense, as it would have to to justify the complexity.
>


Well, the class presents a general solution. Maybe Null could be a valid
value. But you're right, for sake of simplicity I think it's better to
get rid of the boolean variable. If this is the case, we have a
'Nullable' class (see Dot Net version at
http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx)


> --
> Lew



Regards

--
Cholo Lennon
Bs.As.
ARG
 
Reply With Quote
 
Gene Wirchenko
Guest
Posts: n/a
 
      06-17-2011
On Fri, 17 Jun 2011 02:38:34 -0700 (PDT), Paul Cager
<(E-Mail Removed)> wrote:

>On Jun 16, 3:30*pm, Cholo Lennon <(E-Mail Removed)> wrote:
>> IMHO using exceptions is not always the best answer to this problem (as
>> other suggested).


>Not always, no. But in this case there are hints that the OP regarded
>the lack of a String as an error condition - he used phrases such as
>"mean failure" and "DidItWork". In that case I think an exception is
>appropriate (and less work than many of the other suggestions!).


It would be an error in the contexts that I was thinking of, but
that does not make exceptions the way to go.

I now also have a method for getting the next non-blank, non-tab
character from a line. A character being found is correct when
parsing
$quotes "" '' [] ~
and picking up the quotes characters. However, I also use this method
to check for characters after the logical statement end, and in this
case, finding such a character is an error as when parsing
$include "testin.dat" x
(the "x" being wrong).

I do not like the idea that I might have the normal flow of
execution going through a catch block.

[snip]

Sincerely,

Gene Wirchenko
 
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