Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2005

Tip: Looking for answers? Try searching our database.

expression and statements in Java

Thread view: 
Shin - 28 Jul 2005 23:23 GMT
Would it be good to allow me to write either:

<cond> ? <code1> : <code2>

or

if(<cond>) then <code1> else <code2>

without forcing me to commit to which context this piece of code will
appear: i.e., expression or statement.

Normally you will choose one or the other, but when you generate code,
sometimes you just don't know.

Now I miss functional lang.

Suggestions? thanks,
Joan - 29 Jul 2005 00:27 GMT
> Would it be good to allow me to write either:
>
[quoted text clipped - 15 lines]
>
> Suggestions? thanks,

Do what you want to do, who's stopping you?
Roedy Green - 29 Jul 2005 04:38 GMT
>Would it be good to allow me to write either:
>
><cond> ? <code1> : <code2>

in what context are you writing this?  

Signature

Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes

Lasse Reichstein Nielsen - 29 Jul 2005 18:06 GMT
> Would it be good to allow me to write either:
>
[quoted text clipped - 6 lines]
> without forcing me to commit to which context this piece of code will
> appear: i.e., expression or statement.

No. It depends on what <code1> and <code2> are. If they are statements,
then you must use "if", and *should* know that it's the only reasonable
context. If they are expressions, and their values are relevant, then
you should us "?:".

The contents of the branches determines the context, and you should
know what they are.

> Normally you will choose one or the other, but when you generate code,
> sometimes you just don't know.

If you don't know when you generate the code, when will you know?

> Now I miss functional lang.

... which is easier only because everything is an expression. :)

/L
Signature

Lasse Reichstein Nielsen  -  lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
 'Faith without judgement merely degrades the spirit divine.'

Shin - 29 Jul 2005 18:37 GMT
> > Normally you will choose one or the other, but when you generate code,
> > sometimes you just don't know.
>
> If you don't know when you generate the code, when will you know?

Without getting into two much detail, here is the thing: when you are
tranversing an abstract syntax tree, you see a function call node, you
want to generate code so that two function calls are guarded by a
condition, i.e.,

<cond> ? foo() : bar()

Depends on the design of the AST, you may not easily figure out this
function call appears in an expression or statement context.  I am not
saying it's impossilbe; I am saying if we can blend expr and statement,
it will be a lot easier in this situation.  And I don't think
side-effect argument (i.e., Java disallows this because it would be
hard to manage side-effects if otherwise) apply here.

-Shin
Tor Iver Wilhelmsen - 30 Jul 2005 13:16 GMT
> <cond> ? <code1> : <code2>

Here, <code1> and <code2> must be expressions evaluating to values of
the same type - these are returned by the expression.

> if(<cond>) then <code1> else <code2>

while here they are statements that don't (need to) evaluate to
anything. Nothing is returned by the if statement.

So you use the former when it is wise, and the latter in most other
cases since it doesn't have the restrictions of the former.

In Lisp, the if function acts like the first case, apart from the lack
of strict typing.

(if (condition_expression) (true_expression) (false_expression))
Thomas Hawtin - 30 Jul 2005 14:21 GMT
>><cond> ? <code1> : <code2>
>
> Here, <code1> and <code2> must be expressions evaluating to values of
> the same type - these are returned by the expression.

Not strictly accurate, even for reference types. In old versions of the
language, either the static type of <code1> must be assignable from the
static type of <code2>, or vice versa. Kind of handy if you are using
null. From 1.5 the resultant static type is the intersection of <code1>
and <code2> which may not be expressible in Java (except as a generic
parameter).

Be very conservative generating code. Cool tricks to shorten object code
probably are not worthwhile.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.