Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #19495

Re: Assigning void

From Wanja Gayk <brixomatic@yahoo.com>
Newsgroups comp.lang.java.programmer
Subject Re: Assigning void
Date 2012-10-25 01:02 +0200
Organization Netfront http://www.netfront.net/
Message-ID <MPG.2af291cb5001928098973d@202.177.16.121> (permalink)
References (1 earlier) <aegppoFqs6uU1@mid.dfncis.de> <mn.ab7c7dcaa9122664.70216@a.com> <k6232s$tr8$1@dont-email.me> <MPG.2af267d61a8fbe8298973b@202.177.16.121> <k69nlo$ev$1@dont-email.me>

Show all headers | View raw


In article <k69nlo$ev$1@dont-email.me>, Eric Sosman (esosman@comcast-
dot-net.invalid) says...
> 
> On 10/24/2012 4:03 PM, Wanja Gayk wrote:
> > In article <k6232s$tr8$1@dont-email.me>, Eric Sosman (esosman@comcast-
> > dot-net.invalid) says...
> >
> >>       Seriously, the fact that C permits the ?: operator with void
> >> operands is an historical accident
> >
> > Could you elaborate that in more depth please? What's so bad about it in
> > your opinion?

[..]

>      So: The "historical accident" is that `void' was a latecomer,
> and the rules were warped to preserve the investment in old code.

So far so good, "void" was, as many people cite nowadays, a million 
dollar mistake. Still to that point, I find the terse expression quite 
useful.
 
>      To your second question, I don't think it's as "bad" as all
> that, but neither do I think allowing `void' operands is "good."
> In Java we can say that all operators take actual values as
> their operands and yield actual values as their results

Which is violated by the "void" return parameter in the first place, so 
that can't be a sane base for any assumption regarding Java language 
constructs.

> and the ability to say "all" instead of "all except" seems to me a 
> sign of cleanliness in the language.  

Only that we can't say "all" instead of "all except", since Java already 
knows void methods and I don't see that this will change in the near 
future.

[..]

>(Note that `new' is not an
> operator, but a keyword introducing a "class instance creation
> expression," JLS 15.9.  Even [] and (type), which seem very
> operator-like to me, are not called "operators" in JLS.)

I use to regard "new" as a special kind of method call.

>      Also, it appears that `void' is not a "type" in Java.  That
> may be a controversial position (i.e., I may have to eat my
> words), but note that `void' is not listed as a type anywhere
> in JLS 4.  The description of java.lang.Void says it's a place-
> holder for "the Java *keyword* [emphasis mine] void," not for
> a type.  So `void' has an entirely different status in Java than
> in C, where it's a full-fledged (but rather special) "type."  An
> argument that `void' in Java should behave like `void' in C seems
> to me to be on shaky ground; one might make a similar argument
> about `goto'!

However, if Java language designers were, at some point in time, to 
autobox "void" to "Void", having the compiler or JIT generate the 
missing "return null;" calls, which I would like to be honest, then we 
would be back where we started with C: Every method had a return value, 
and the ternary operator would be applicable everywhere.

In case you're interested, here's a blog I once wrote regarding the Void 
class:
> http://brixomatic.wordpress.com/2011/07/17/into-the-void-refining-
java-beans-with-rare-weapons/ <


>      Finally, Java's lack of a C-style preprocessor removes some
> of the incentive for hyper-abbreviated logic.  In C macros one
> sometimes has a need to cram what would ordinarily be a statement
> into a context where an expression is required, and both the
> `void'-operand ?: and , operators are handy for this purpose, if
> more than a little obfuscatory.  Take away the preprocessor and
> the need for that kind of condensation, and you take away most of
> the reasons for not writing if/then/else in the first place.

I do think that the ternary operator for void (or Void) methods can look 
pretty sexy, which means: readable. It could even be more readable and 
more versatile than a switch statement for example:

  order.isPlaced() ? mailCustomer(confirmation(order))
: order.isSent() ? mailCustomer(trackingInfo(order))
: order.isDelivered() ? mailCustomer(thankYouForOrdering(order));
: throw new IllegalStateException(order.toString());

compare that to:
if(order.isPlaced()) mailCustomer(confirmation(order));
else if(order.isSent()) mailCustomer(trackingInfo(order));
else if(order.isDelivered()) mailCustomer(thankYouForOrdering(order));
else throw new IllegalStateException(order.toString());

or
switch(order.getState()){
  case PLACED: mailCustomer(confirmation(order)); 
   break;
  case SENT: mailCustomer(trackingInfo(order));
   break;
  case DELIVERED: mailCustomer(thankYouForOrdering(order));
   break;
  default:
   throw new IllegalStateException(order.toString());
}

I regard the ternary expression more readable.

Note that this is just some stuff that sprung to my mind, I know that 
one could express the same with polymorphism for example.#

Funny thing is: If you made all these methods return "Void" instead of 
void, you could use the ternary operator like this:

Void _ =
  order.isPlaced() ? mailCustomer(confirmation(order))
: order.isSent() ? mailCustomer(trackingInfo(order))
: order.isDelivered() ? mailCustomer(thankYouForOrdering(order))
: throw new IllegalStateException(order.toString());

It's a dirty hack, I know. But it's pretty interesting what you can do, 
if you like.

Kind regards,
Wanja

-- 
..Alesi's problem was that the back of the car was jumping up and down 
dangerously - and I can assure you from having been teammate to 
Jean Alesi and knowing what kind of cars that he can pull up with, 
when Jean Alesi says that a car is dangerous - it is. [Jonathan Palmer]

--- news://freenews.netfront.net/ - complaints: news@netfront.net ---

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Assigning void Wojtek <nowhere@a.com> - 2012-10-12 22:54 -0700
  Re: Assigning void Lew <lewbloch@gmail.com> - 2012-10-12 23:27 -0700
    Re: Assigning void Wojtek <nowhere@a.com> - 2012-10-13 12:35 -0700
  Re: Assigning void Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-10-13 09:26 -0400
  Re: Assigning void Donkey Hottie <donkey@fredriksson.dy.fi> - 2012-10-13 17:06 +0300
    Re: Assigning void Jeff Higgins <jeff@invalid.invalid> - 2012-10-13 10:34 -0400
    Re: Assigning void Wanja Gayk <brixomatic@yahoo.com> - 2012-10-13 22:33 +0200
      Re: Assigning void Jeff Higgins <jeff@invalid.invalid> - 2012-10-13 17:34 -0400
      Re: Assigning void Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-10-13 17:45 -0400
        Re: Assigning void Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-10-13 21:35 -0400
        Re: Assigning void Wanja Gayk <brixomatic@yahoo.com> - 2012-10-16 00:16 +0200
  Re: Assigning void Jeff Higgins <jeff@invalid.invalid> - 2012-10-13 11:59 -0400
    Re: Assigning void glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-10-13 17:05 +0000
      Re: Assigning void Jeff Higgins <jeff@invalid.invalid> - 2012-10-13 14:26 -0400
        Re: Assigning void markspace <-@.> - 2012-10-13 11:51 -0700
          Re: Assigning void Jeff Higgins <jeff@invalid.invalid> - 2012-10-13 15:08 -0400
          Re: Assigning void Wojtek <nowhere@a.com> - 2012-10-13 12:28 -0700
            Re: Assigning void markspace <-@.> - 2012-10-13 12:42 -0700
              Re: Assigning void Lew <lewbloch@gmail.com> - 2012-10-13 12:54 -0700
                OT - Trolling Wojtek <nowhere@a.com> - 2012-10-13 14:16 -0700
                Re: OT - Trolling Jeff Higgins <jeff@invalid.invalid> - 2012-10-13 18:09 -0400
                Re: OT - Trolling Gene Wirchenko <genew@ocis.net> - 2012-10-14 18:47 -0700
              Re: Assigning void Martin Gregorie <martin@address-in-sig.invalid> - 2012-10-13 20:28 +0000
              Re: Assigning void Wojtek <nowhere@a.com> - 2012-10-13 13:31 -0700
                Re: Assigning void Joerg Meier <joergmmeier@arcor.de> - 2012-10-20 18:00 +0200
                Re: Assigning void Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2012-10-20 09:56 -0700
                Re: Assigning void "Peter J. Holzer" <hjp-usenet2@hjp.at> - 2012-10-21 13:24 +0200
                Re: Assigning void Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2012-10-21 10:23 -0700
                Re: Assigning void "Peter J. Holzer" <hjp-usenet2@hjp.at> - 2012-10-22 00:13 +0200
                Re: Assigning void Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2012-10-21 16:25 -0700
                Re: Assigning void "Peter J. Holzer" <hjp-usenet2@hjp.at> - 2012-10-22 08:43 +0200
                Re: Assigning void "Peter J. Holzer" <hjp-usenet2@hjp.at> - 2012-10-22 10:18 +0200
                Re: Assigning void Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2012-10-22 08:14 -0700
                Re: Assigning void glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-10-21 19:45 +0000
                Re: Assigning void "Peter J. Holzer" <hjp-usenet2@hjp.at> - 2012-10-22 00:14 +0200
            Re: Assigning void Lew <lewbloch@gmail.com> - 2012-10-13 12:44 -0700
              Re: Assigning void Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2012-10-15 14:38 +0300
                Re: Assigning void Lew <lewbloch@gmail.com> - 2012-10-15 09:11 -0700
        Re: Assigning void Jeff Higgins <jeff@invalid.invalid> - 2012-10-13 15:03 -0400
          Re: Assigning void glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-10-13 20:21 +0000
    Re: Assigning void Lew <lewbloch@gmail.com> - 2012-10-13 12:38 -0700
      Re: Assigning void markspace <-@.> - 2012-10-13 12:49 -0700
        Re: Assigning void Lew <lewbloch@gmail.com> - 2012-10-13 13:03 -0700
          Re: Assigning void Robert Klemme <shortcutter@googlemail.com> - 2012-10-14 14:09 +0200
            Re: Assigning void Lew <lewbloch@gmail.com> - 2012-10-14 11:31 -0700
      Re: Assigning void Jeff Higgins <jeff@invalid.invalid> - 2012-10-13 19:55 -0400
        Re: Assigning void Jeff Higgins <jeff@invalid.invalid> - 2012-10-13 20:25 -0400
          Re: Assigning void Jeff Higgins <jeff@invalid.invalid> - 2012-10-13 21:01 -0400
            Re: Assigning void Gene Wirchenko <genew@ocis.net> - 2012-10-14 18:42 -0700
  Re: Assigning void Roedy Green <see_website@mindprod.com.invalid> - 2012-10-14 00:51 -0700
  Re: Assigning void Jeff Higgins <jeff@invalid.invalid> - 2012-10-14 10:40 -0400
  Re: Assigning void Sven Köhler <remove-sven.koehler@gmail.com> - 2012-10-21 01:24 +0200
    Re: Assigning void Wojtek <nowhere@a.com> - 2012-10-21 14:52 -0700
      Re: Assigning void Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-10-21 20:13 -0400
        Re: Assigning void Wojtek <nowhere@a.com> - 2012-10-21 20:20 -0700
          Re: Assigning void Sven Köhler <remove-sven.koehler@gmail.com> - 2012-10-22 11:12 +0200
        Re: Assigning void Wanja Gayk <brixomatic@yahoo.com> - 2012-10-24 22:03 +0200
          Re: Assigning void Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-10-24 17:47 -0400
            Re: Assigning void Wanja Gayk <brixomatic@yahoo.com> - 2012-10-25 01:02 +0200
              Re: Assigning void Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-10-24 20:21 -0400
                Re: Assigning void Martin Gregorie <martin@address-in-sig.invalid> - 2012-10-25 20:04 +0000
                (OT) Re: Assigning void Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-10-25 16:31 -0400
                Re: (OT) Re: Assigning void Martin Gregorie <martin@address-in-sig.invalid> - 2012-10-25 20:44 +0000
            Re: Assigning void Arved Sandstrom <asandstrom2@eastlink.ca> - 2012-10-25 07:25 -0300
              Re: Assigning void Lew <lewbloch@gmail.com> - 2012-10-25 11:12 -0700
      Re: Assigning void Sven Köhler <remove-sven.koehler@gmail.com> - 2012-10-22 11:01 +0200
      Re: Assigning void Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2012-10-23 00:38 +0200

csiph-web