News for hackers. Stuff that matters.
Re:Statements vs. Expressions by bluepnume on Thursday July 14, 2022 @05:43AM (#32093909)
If this compiles, <something> is an expression:

    let x = <something>;
If this compiles, <something> is a statement:

    <something>;
Both, either, or neither can be true.
Re:Statements vs. Expressions by hibbelig on Thursday July 14, 2022 @05:59AM (#32094002)
I can't think of an example where

    let x = <something>;
compiles but

    <something>;
does not compile. I think all expressions are statements.
Re:Statements vs. Expressions by lmm on Thursday July 14, 2022 @06:40AM (#32094216)
Some languages forbid "obviously pure" expressions like "0" as statements (since using them as a statement is probably a mistake).
Re:Statements vs. Expressions by hibbelig on Friday July 15, 2022 @09:37AM (#32108047)
I think I was saying that Javascript is not one of them.
Re:Statements vs. Expressions by mxben on Thursday July 14, 2022 @07:36AM (#32094591)
Interesting! Any examples of such languages?
Re:Statements vs. Expressions by lmm on Thursday July 14, 2022 @05:14PM (#32101542)
Java is one popular example. You get "error: not a statement".
Re:Statements vs. Expressions by bluepnume on Thursday July 14, 2022 @08:03PM (#32102878)
Right. They're not mutually exclusive -- hence the "both, either, or neither"
Re:Statements vs. Expressions by hibbelig on Friday July 15, 2022 @09:37AM (#32108041)
My point was that there are four combinations, and "both, either, or neither" suggests that all four can happen. But actually, only three out of four can happen.

It can be both a statement and an expression. Yes.

It can be neither a statement nor an expression. Yes.

It can be a statement but not an expression. Yes.

But the case that it is an expression but not a statement: this case never happens.

Re:Statements vs. Expressions by FeepingCreature on Thursday July 14, 2022 @05:50AM (#32093959)
So 'if', 'for', 'while' are not statements?
Re:Statements vs. Expressions by lmm on Thursday July 14, 2022 @06:38AM (#32094207)
Not on their own. A full if, for or while (with the conditions and body) is usally a statement, and might be an expression in some languages.
Re:Statements vs. Expressions by FeepingCreature on Friday July 15, 2022 @06:17AM (#32106580)
The body is also not terminated by a semicolon.

`while (true) { puts("Hello World\n"); };`

Re:Statements vs. Expressions by lmm on Friday July 15, 2022 @06:50AM (#32106806)
What you've written there does compile though, which was the original condition :).
Re:Statements vs. Expressions by FeepingCreature on Saturday July 16, 2022 @05:06AM (#32116349)
I get the joke, but it's two statements, not one. (while and ;)
Re:Statements vs. Expressions by shikoba on Thursday July 14, 2022 @07:05AM (#32094368)
In JavaScript no. But in some languages it can be.
Re:Statements vs. Expressions by teddyh on Thursday July 14, 2022 @06:20AM (#32094114)
No. Those are syntactic elements.