Assigning void

W

Wojtek

Sven Köhler wrote :
Am 13.10.2012 07:54, schrieb Wojtek:

That's not correct Java syntax. Live with it.

So you just posted your first message to this group and this is how you
want to represent yourself?

Of course it is not valid syntax, I was just trying to start a
discussion about its possible merits.
 
P

Peter J. Holzer

31:58 -0700, Wojtek wrote:
int compare = (a > b) ? 1 : (a < b) ? -1 : 0; [...might mean...]
int compare = ((a > b) ? 1 : (a < b)) ? -1 : 0;

It can't be that one. That expression doesn't compile, because the literal
"1" is not a boolean value, nor is "(a < b)" an integer value, and so "1 :
(a < b)" is not a valid portion of a ?: operator expression (i.e. the two
possible results don't match in type, nor are convertible to each other's
type).

True. However, in C there is (was) no boolean type so the precedence
could still be the wrong way around (requiring extra parentheses in
Java).

Huh? C != Java. Deficiencies in C don't cause problems in Java. Only
deficiencies in Java do (including those Java may have borrowed from
C...but that is not the case here).

Yes, but the syntax originated in C and Java didn't change it. While
Java did change some aspects (e.g., it allows assignments only at top of
a statement expression, not nested within an expression), it didn't
change the relative precedence or associativity of operators - it didn't
even fix the priority of the bitwise logical operators (which dmr
himself said was wrong in C (as Glen mentioned, it's wrong because the
logical operators && and || were a later addition and by then they
couldn't change an existing operator)). So if dmr had gotten ?: wrong,
Java probably wouldn't have fixed that, either.

hp
 
P

Peter J. Holzer

I suppose, but some of the C precedence rules are the result of
the late addition of the logical operators && and ||.

Previously, the & and | operators were used, and they kept their
precedence even after && and || were added.

Yes. I thought of mentioning that but omitted it for the sake of
brevity.

hp
 
E

Eric Sosman

Sven Köhler wrote :

So you just posted your first message to this group and this is how you
want to represent yourself?

Of course it is not valid syntax, I was just trying to start a
discussion about its possible merits.

Merits: "If you want Lisp, you know where to find it."

Seriously, the fact that C permits the ?: operator with void
operands is an historical accident, a holdover from before the days
when `void' came to C. Java lacks C's historical baggage, so there's
little reason to carry it. (Java slavishly perpetuates a few of C's
other infelicities, but this one isn't among them.)
 
W

Wojtek

Eric Sosman wrote :
Merits: "If you want Lisp, you know where to find it."

Seriously, the fact that C permits the ?: operator with void
operands is an historical accident, a holdover from before the days
when `void' came to C. Java lacks C's historical baggage, so there's
little reason to carry it. (Java slavishly perpetuates a few of C's
other infelicities, but this one isn't among them.)

The funny thing is that I had not ever used a "void" assignment in C. I
was just coding along and I thought that using the ?: operator would be
a "neat" thing. Then the compiler complained so I changed it to an
if/else statement.

But then I started to wonder, so I posted here.

Call it idle curiosity...
 
P

Peter J. Holzer

Yes, but the syntax originated in C and Java didn't change it. While
Java did change some aspects [...]

Sorry. I don't see how you can "change some aspects" but still not "change
it".

Sorry. That should have read:

The syntax of the ConditionalExpression[1] originated in C and Java
didn't change it. While Java did change some aspects of expression
syntax in general ...

The given example in Java is crystal clear, given that only one
interpretation of the expression can even compile.

That it's ambiguous in C without knowing of operator precedence is
irrelevant. It's not ambiguous in Java.

It could still be an error in Java. Java syntax is fixed. The compiler
doesn't go back and try some other other parse tree if the code violates
some semantic constraint.

Yes, the code is unambiguous. But it is unambiguous because the syntax
is what it is. And the syntax is what it is because it was the same way
in C. If it had been different in C, it would (probably) also be
different in Java and then the code *wouldn't* compile.

hp

[1] http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.25
 
P

Peter J. Holzer

It could still be an error in Java. Java syntax is fixed. The compiler
doesn't go back and try some other other parse tree if the code violates
some semantic constraint.

Yes, the code is unambiguous. But it is unambiguous because the syntax
is what it is. And the syntax is what it is because it was the same way
in C. If it had been different in C, it would (probably) also be
different in Java and then the code *wouldn't* compile.

Here's an example which (hopefully) illustrates what I mean:

C got the relative precedence of the bitwise operators and the
comparison operators wrong: & has lower precedence than ==, although
that's usually not what you want. That could have been fixed in Java,
but it wasn't.

So given three int variables a, b, and m, consider this expression:

a & m == b & m

This is parsed as

(a & (m == b)) & m

which then produces the error message

The operator & is undefined for the argument type(s) int, boolean

The compiler can't go back and parse it as

(a & m) == (b & m)

even though that would make sense and is almost certainly what the
programmer meant. That's just not how Java (or any other language with a
context free grammar) works.

So even though one of the interpretations doesn't even compile, the one
which doesn't compile is actually the correct one and the the one which
would compile is wrong.

hp
 
S

Sven Köhler

Am 21.10.2012 23:52, schrieb Wojtek:
Sven Köhler wrote :

So you just posted your first message to this group and this is how you
want to represent yourself?

Your original post read like you were complaining that the syntax you
came up with was not allowed.
Of course it is not valid syntax, I was just trying to start a
discussion about its possible merits.

So you would like to see this syntax added to Java? There are no merits
beside saving a few characters of source. Not worth it.


Regards,
Sven
 
S

Sven Köhler

Am 22.10.2012 05:20, schrieb Wojtek:
The funny thing is that I had not ever used a "void" assignment in C. I
was just coding along and I thought that using the ?: operator would be
a "neat" thing. Then the compiler complained so I changed it to an
if/else statement.

In C, the line "123;" is correct syntax. (At least, gcc allows it. I
didn't do more research.)

In Java, no such thing is allowed. The syntax forbids it. Hence, the ?:
operator must be used in a place, where some sort of value is required.
For example on the right side of an assignment, or a parameter when a
method is called.


Regards,
Sven
 
D

Daniele Futtorovic

Sven Köhler wrote :

So you just posted your first message to this group and this is how you
want to represent yourself?

And what a flamboyant entrance that was! Succinctly settles the
question. No fluff, no aside, not remainder. Me like. :)
 
E

Eric Sosman

Could you elaborate that in more depth please? What's so bad about it in
your opinion?

To the first question: Original C had no `void' type at all.
When you had a function (Java: "method") that returned nothing,
it was actually written as if it returned `int'. The caller was
supposed to ignore whatever garbage happened to land in the
"function value goes here" place (often a designated CPU register),
and that was that.

Since the compiler couldn't tell a sort-of-`int' function
from a really-`int' function, it had to permit calls to either
kind as the second and third operand of the ?: operator -- since
the two kinds were entirely equivalent, there was no way to forbid
the former while allowing the latter. Some C programmers found
they could use ?: as a sort of "one-line if/then/else", and did
so. The condensation was occasionally useful in macro expansions,
and besides: We old-line C programmers value terseness above all
else, even speed (and we value both above correctness ;).

Along came ANSI C, bearing `void' along with other gifts,
and it became possible to distinguish the two kinds of function.
The compiler could squawk if a non-`void' function failed to
return a value, or if a caller tried to use the non-existent
value of a `void' function. It would also have been possible
to forbid `void' operands for ?:, but one of the Committee's
charges was to invalidate as little existing code as possible.
So they "grandfathered" existing practice by making a special
case: the second and third operands could be compatible types,
or they could both be `void'. (I think this makes ?: one of
only two C operators that can take a `void' operand, the other
being the , operator.)

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

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, and the
ability to say "all" instead of "all except" seems to me a sign
of cleanliness in the language. (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.)

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'!

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.

private final class FlameResistantSuit
extends WhatIWasWearingBefore { ... }
 
E

Eric Sosman

Eric Sosman (esosman@comcast- said:
[...]
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.

I'm not entirely sure what you mean. A method can be
declared as `void', which (in Java) means it returns nothing
at all, not even some C-ish non-value of a vacuous type. The
violation you mention escapes my understanding.
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.

"Things would be so di-if-fe-rent,
If they were not as they are!"
-- Anna Russell
(Extra credit if you know the tune.)
 
A

Arved Sandstrom

Could you elaborate that in more depth please? What's so bad about it in
your opinion?
[ SNIP ]

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'!
[ SNIP ]

My reading of the JLS leads me to the same conclusion as you, that
"void" is not a Java type (primitive). As you pointed out, it's a keyword.

I figure it's best to stay away from type theory with 'void' and 'Void'.
Most people that do stray down this road think of Java 'void' as being
somewhat like an empty (zero/bottom) type. But since a bottom type means
the lack of a normal return, Java 'void' isn't probably a real empty
type, and I've seen some folks say that it's more of a unit type. And if
you really, really have to draw that wrapper parallel between 'void' and
java.lang.Void, and 'Void''s' uses in reflection and generics, that
seems to reinforce the interpretation that the unit type concept is
perhaps more apt.

And then you've got void.class, of which the type is Class<Void>. :)

My way of thinking about it - if type theory had to be brought into it -
is that both 'void' and 'Void' are unit types that express "nothing that
matters to me".

AHS
 
L

Lew

Arved said:
Eric Sosman wrote:
[ SNIP ]
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'!

[ SNIP ]

My reading of the JLS leads me to the same conclusion as you, that
"void" is not a Java type (primitive). As you pointed out, it's a keyword.

I figure it's best to stay away from type theory with 'void' and 'Void'.
Most people that do stray down this road think of Java 'void' as being
somewhat like an empty (zero/bottom) type. But since a bottom type means
the lack of a normal return, Java 'void' isn't probably a real empty
type, and I've seen some folks say that it's more of a unit type. And if
you really, really have to draw that wrapper parallel between 'void' and
java.lang.Void, and 'Void''s' uses in reflection and generics, that
seems to reinforce the interpretation that the unit type concept is
perhaps more apt.

And then you've got void.class, of which the type is Class<Void>. :)

My way of thinking about it - if type theory had to be brought into it -
is that both 'void' and 'Void' are unit types that express "nothing that
matters to me".

Ever since generics entered Java, thinking in terms of type theory has made sense
for Java programming, not that it didn't before.

Arved's analysis gives signposts for further research, at least I will take it that way.
 
E

Eric Sosman

I don't know the tune, but you're the first person I've encountered in a
very long time who knows who Anna Russell was. Long ago and far away I
was fortunate enough to see her perform her single handed demolition of
the Ring Cycle.

Grew up with her recordings, and saw her live a couple times.
Most memorable moment: She led the entire Symphony Hall (Boston)
audience in a sing-along of one of the choruses from "Nabucco,"
with English words adapted from a spiritual concerning Meshach,
Shadrach, and Abednego.

(I'm not making this up, you know.)

The quoted couplet is from her presentation on "How to Write
Your Own Gilbert and Sullivan Opera."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top