Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Assigning void

Reply
Thread Tools

Assigning void

 
 
Wojtek
Guest
Posts: n/a
 
      10-13-2012
I can legally do the following:

if (server.getConnectionType() == ConnectionType.ACTIVE)
ftp.enterLocalActiveMode();
else
ftp.enterLocalPassiveMode();

but I want to do:

(server.getConnectionType() == ConnectionType.ACTIVE) ?
ftp.enterLocalActiveMode() : ftp.enterLocalPassiveMode();

However the compiler insists that I must have an assignment even though
both ftp methods return void. I would think that the compiler would be
smart enough to realize that nothing CAN be returned.

Oh yes, I am using Eclipse with Java 7

Thoughts? I mean other than stylistic comments...

--
Wojtek


 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      10-13-2012
Wojtek wrote:
> I can legally do the following:
>
> if (server.getConnectionType() == ConnectionType.ACTIVE)
> ftp.enterLocalActiveMode();
> else
> ftp.enterLocalPassiveMode();
>
> but I want to do:
>
> (server.getConnectionType() == ConnectionType.ACTIVE) ?
> ftp.enterLocalActiveMode() : ftp.enterLocalPassiveMode();
>
> However the compiler insists that I must have an assignment even though
> both ftp methods return void. I would think that the compiler would be
> smart enough to realize that nothing CAN be returned.
>
> Oh yes, I am using Eclipse with Java 7
>
> Thoughts? I mean other than stylistic comments...


The compiler is smart enough to realize that nothing can be returned.

It's also smart enough to realize that standalone expressions, such
as this one or a lone 'x', are not legal.

http://docs.oracle.com/javase/specs/....html#jls-14.8
and the rest of Chapter 14.

--
Lew
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      10-13-2012
On 10/13/2012 1:54 AM, Wojtek wrote:
> I can legally do the following:
>
> if (server.getConnectionType() == ConnectionType.ACTIVE)
> ftp.enterLocalActiveMode();
> else
> ftp.enterLocalPassiveMode();
>
> but I want to do:
>
> (server.getConnectionType() == ConnectionType.ACTIVE) ?
> ftp.enterLocalActiveMode() : ftp.enterLocalPassiveMode();
>
> However the compiler insists that I must have an assignment even though
> both ftp methods return void. I would think that the compiler would be
> smart enough to realize that nothing CAN be returned.
>
> Oh yes, I am using Eclipse with Java 7
>
> Thoughts? I mean other than stylistic comments...



http://docs.oracle.com/javase/specs/...html#jls-15.25

.... with special attention to the second BOLDFACE sentence.


--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
Jeff Higgins
Guest
Posts: n/a
 
      10-13-2012
On 10/13/2012 10:06 AM, Donkey Hottie wrote:
> 13.10.2012 08:54, Wojtek kirjoitti:
>> I can legally do the following:
>>
>> if (server.getConnectionType() == ConnectionType.ACTIVE)
>> ftp.enterLocalActiveMode();
>> else
>> ftp.enterLocalPassiveMode();
>>
>> but I want to do:
>>
>> (server.getConnectionType() == ConnectionType.ACTIVE) ?
>> ftp.enterLocalActiveMode() : ftp.enterLocalPassiveMode();
>>
>> However the compiler insists that I must have an assignment even though
>> both ftp methods return void. I would think that the compiler would be
>> smart enough to realize that nothing CAN be returned.
>>
>> Oh yes, I am using Eclipse with Java 7
>>
>> Thoughts? I mean other than stylistic comments...
>>

>
> Why do you want to use an assignment statement without need or means to
> assign anything to anywhere?

I'm guessing he doesn’t.
> What's the rationale?

I'm guessing his eye beholds beauty in the latter.


 
Reply With Quote
 
Jeff Higgins
Guest
Posts: n/a
 
      10-13-2012
On 10/13/2012 01:54 AM, Wojtek wrote:
> I can legally do the following:
>
> if (server.getConnectionType() == ConnectionType.ACTIVE)
> ftp.enterLocalActiveMode();
> else
> ftp.enterLocalPassiveMode();
>
> but I want to do:
>
> (server.getConnectionType() == ConnectionType.ACTIVE) ?
> ftp.enterLocalActiveMode() : ftp.enterLocalPassiveMode();
>
> However the compiler insists that I must have an assignment even though
> both ftp methods return void. I would think that the compiler would be
> smart enough to realize that nothing CAN be returned.
>
> Oh yes, I am using Eclipse with Java 7
>
> Thoughts? I mean other than stylistic comments...
>

Others have pointed you to the specification.
I don't know why it can't be a shorthand if-then-else statement too.

 
Reply With Quote
 
glen herrmannsfeldt
Guest
Posts: n/a
 
      10-13-2012
Jeff Higgins <(E-Mail Removed)> wrote:

(snip)

>> (server.getConnectionType() == ConnectionType.ACTIVE) ?
>> ftp.enterLocalActiveMode() : ftp.enterLocalPassiveMode();


(snip)

> Others have pointed you to the specification.
> I don't know why it can't be a shorthand if-then-else statement too.


Java isn't C.

Java allows for method calls that return a value to ignore the
return value, but other expressions that don't use the value
seem not to be allowed.

2+2;

is legal C, but not Java.

-- glen
 
Reply With Quote
 
Jeff Higgins
Guest
Posts: n/a
 
      10-13-2012
On 10/13/2012 01:05 PM, glen herrmannsfeldt wrote:
> Jeff Higgins<(E-Mail Removed)> wrote:
>
> (snip)
>
>>> (server.getConnectionType() == ConnectionType.ACTIVE) ?
>>> ftp.enterLocalActiveMode() : ftp.enterLocalPassiveMode();

>
> (snip)
>
>> Others have pointed you to the specification.
>> I don't know why it can't be a shorthand if-then-else statement too.

>
> Java isn't C.
>
> Java allows for method calls that return a value to ignore the
> return value, but other expressions that don't use the value
> seem not to be allowed.
>
> 2+2;
>
> is legal C, but not Java.
>

C has its Rationale.
Java seems not to have a separate formal one.
6.5.15 of the C Rationale only states
the construct is allowed and not why.
15.25 of the Java Spec. only states
the construct is disallowed and not why.
Does it sound childish to ask why?





 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      10-13-2012
On 10/12/2012 10:54 PM, Wojtek wrote:
> both ftp methods return void. I would think that the compiler would
> be smart enough to realize that nothing CAN be returned.



On 10/13/2012 11:26 AM, Jeff Higgins wrote:
> Does it sound childish to ask why?



The way the OP puts it, yes, it comes across as a bit childish. The OP
didn't ask "why," he asked for "thoughts." My thought is "read the JLS."

I realize *you* asked why, but in the vein of the OP's premise, I feel
"read the spec" is a valid answer. "Why" requires reading someone's
mind, and time travel. Since those are obviously impossible, the result
borders on trolling.

I'd guess that the original intent and the current thinking is that Java
should be "easy to read" and that shortening branch flow to a single
character pushes somebody's Perl buttons, but that's a total guess.

What about this:

(some large expression || some other large expression )
? {
Statement;
Statement;
Statement;
} : {
Statement;
Statement;
Statement;
}

? Do we really want to go there?


 
Reply With Quote
 
Jeff Higgins
Guest
Posts: n/a
 
      10-13-2012
On 10/13/2012 02:26 PM, Jeff Higgins wrote:
> On 10/13/2012 01:05 PM, glen herrmannsfeldt wrote:
>> Jeff Higgins<(E-Mail Removed)> wrote:
>>
>> (snip)
>>
>>>> (server.getConnectionType() == ConnectionType.ACTIVE) ?
>>>> ftp.enterLocalActiveMode() : ftp.enterLocalPassiveMode();

>>
>> (snip)
>>
>>> Others have pointed you to the specification.
>>> I don't know why it can't be a shorthand if-then-else statement too.

>>
>> Java isn't C.
>>
>> Java allows for method calls that return a value to ignore the
>> return value, but other expressions that don't use the value
>> seem not to be allowed.
>>
>> 2+2;
>>
>> is legal C, but not Java.
>>

> C has its Rationale.
> Java seems not to have a separate formal one.
> 6.5.15 of the C Rationale only states
> the construct is allowed and not why.
> 15.25 of the Java Spec. only states
> the construct is disallowed and not why.
> Does it sound childish to ask why?


Don't get me wrong. I understand that in both
languages this construct is the conditional "operator".
I'm asking only why it cannot also be shorthand for
an if-then-else statement.
boolean expression ? block : block

 
Reply With Quote
 
Jeff Higgins
Guest
Posts: n/a
 
      10-13-2012
On 10/13/2012 02:51 PM, markspace wrote:
> On 10/12/2012 10:54 PM, Wojtek wrote:
>> both ftp methods return void. I would think that the compiler would
>> be smart enough to realize that nothing CAN be returned.

>
>
> On 10/13/2012 11:26 AM, Jeff Higgins wrote:
>> Does it sound childish to ask why?

>
>
> The way the OP puts it, yes, it comes across as a bit childish. The OP
> didn't ask "why," he asked for "thoughts." My thought is "read the JLS."
>
> I realize *you* asked why, but in the vein of the OP's premise, I feel
> "read the spec" is a valid answer. "Why" requires reading someone's
> mind, and time travel. Since those are obviously impossible, the result
> borders on trolling.
>
> I'd guess that the original intent and the current thinking is that Java
> should be "easy to read" and that shortening branch flow to a single
> character pushes somebody's Perl buttons, but that's a total guess.
>
> What about this:
>
> (some large expression || some other large expression )
> ? {
> Statement;
> Statement;
> Statement;
> } : {
> Statement;
> Statement;
> Statement;
> }
>
> ? Do we really want to go there?


I suppose it would be allowed in my
boolean expression ? block : block ;
and would probably be ugly per your example.

But I think
(server.getConnectionType() == ConnectionType.ACTIVE) ?
ftp.enterLocalActiveMode() : ftp.enterLocalPassiveMode();
would be pretty.

 
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
What is the difference between void proba(); and void proba(void); ??? PencoOdStip@gmail.com C++ 1 05-23-2007 07:12 PM
Assigning methods to objects, and assigning onreadystatechange to an XMLHttpRequest -- an inconsistency? weston Javascript 1 09-22-2006 09:33 AM
what is the difference, void func(void) and void fucn() noblesantosh@yahoo.com C Programming 5 07-22-2005 04:38 PM
"void Method()" vs "void Method(void)" Ollej Reemt C++ 7 04-22-2005 03:47 AM
`void **' revisited: void *pop(void **root) Stig Brautaset C Programming 15 10-28-2003 09:03 AM



Advertisments