www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Semicolon?

reply Arun <aruncxy gmail.com> writes:
Just curious. Does the compiler need semicolon anymore? Are there 
corner cases that don't work without semicolon?
Mar 31 2021
next sibling parent reply deadalnix <deadalnix gmail.com> writes:
On Wednesday, 31 March 2021 at 21:51:07 UTC, Arun wrote:
 Just curious. Does the compiler need semicolon anymore? Are 
 there corner cases that don't work without semicolon?
They aren't strictly needed most of the time, but they do make things much clearer, especially when you have a syntax error.
Mar 31 2021
next sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 3/31/2021 2:59 PM, deadalnix wrote:
 On Wednesday, 31 March 2021 at 21:51:07 UTC, Arun wrote:
 Just curious. Does the compiler need semicolon anymore? Are there corner cases 
 that don't work without semicolon?
They aren't strictly needed most of the time, but they do make things much clearer, especially when you have a syntax error.
Yup. They're a form of redundancy like a parity bit is.
Mar 31 2021
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 3/31/21 10:13 PM, Walter Bright wrote:
 On 3/31/2021 2:59 PM, deadalnix wrote:
 On Wednesday, 31 March 2021 at 21:51:07 UTC, Arun wrote:
 Just curious. Does the compiler need semicolon anymore? Are there 
 corner cases that don't work without semicolon?
They aren't strictly needed most of the time, but they do make things much clearer, especially when you have a syntax error.
Yup. They're a form of redundancy like a parity bit is.
There are a few cases that would be ambiguous. Not sure how realistic: auto a = b; *c; Without semicolons the semantics would be different.
Mar 31 2021
next sibling parent Walter Bright <newshound2 digitalmars.com> writes:
On 3/31/2021 7:40 PM, Andrei Alexandrescu wrote:
 There are a few cases that would be ambiguous. Not sure how realistic:
 
 auto a = b;
 *c;
 
 Without semicolons the semantics would be different.
D disallows pointless expressions like *c; so it would parse as b*c. Not really helpful in diagnosing the problem. It's one of those perennial good ideas, like implicit declarations, that resurface every couple of years. It isn't obvious why they are a bad idea, until you work with them for a while.
Apr 01 2021
prev sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 3/31/21 10:40 PM, Andrei Alexandrescu wrote:
 On 3/31/21 10:13 PM, Walter Bright wrote:
 On 3/31/2021 2:59 PM, deadalnix wrote:
 On Wednesday, 31 March 2021 at 21:51:07 UTC, Arun wrote:
 Just curious. Does the compiler need semicolon anymore? Are there 
 corner cases that don't work without semicolon?
They aren't strictly needed most of the time, but they do make things much clearer, especially when you have a syntax error.
Yup. They're a form of redundancy like a parity bit is.
There are a few cases that would be ambiguous. Not sure how realistic: auto a = b; *c; Without semicolons the semantics would be different.
Languages without semicolons generally use some other delimiter, usually newline. The nice thing about semicolons is that you can pick whatever format you want, so whitespace can be done however you want. But it's a curse as well. I've been recently teaching a class with D to homeschool kids with super-limited coding experience (in fact, everything they know they learned from me pretty much). We started with Javascript (optional semicolons), and lua ("begin/end" tags), and now on D, I find one of the most common errors that they have a hard time solving is a lack of semicolon. I don't know if this is specific to D, but I do notice that forgetting punctuation usually produces a wall of errors, of which only the first one is relevant (usually). When you have not been programmed to write punctuation in coding properly, the oddest things come out. I sometimes spend minutes trying to figure out why their code is not compiling, when I finally notice a stray closing curly brace somewhere off in the distance. Not sure if there's a way to improve it. -Steve
Apr 01 2021
parent reply jmh530 <john.michael.hall gmail.com> writes:
On Thursday, 1 April 2021 at 16:02:48 UTC, Steven Schveighoffer 
wrote:
 [snip]

 I don't know if this is specific to D, but I do notice that 
 forgetting punctuation usually produces a wall of errors, of 
 which only the first one is relevant (usually). When you have 
 not been programmed to write punctuation in coding properly, 
 the oddest things come out. I sometimes spend minutes trying to 
 figure out why their code is not compiling, when I finally 
 notice a stray closing curly brace somewhere off in the 
 distance.

 Not sure if there's a way to improve it.

 -Steve
Yeah that's an annoying thing to catch. Reminds me of the video with the basketball players passing the ball as the guy in the gorilla suit walks through, the semicolon is just not the thing you're focusing on sometimes. The only thing I can think of is that when the compiler sees an error, it can check if the prior line ends with a semicolon, then it can check if the error would be generated if a semicolon is added. Finally, display an error about missing a semicolon.
Apr 01 2021
parent reply evilrat <evilrat666 gmail.com> writes:
On Thursday, 1 April 2021 at 17:10:02 UTC, jmh530 wrote:
 The only thing I can think of is that when the compiler sees an 
 error, it can check if the prior line ends with a semicolon, 
 then it can check if the error would be generated if a 
 semicolon is added. Finally, display an error about missing a 
 semicolon.
I think in languages where it is optional the rules are a bit different. e.g. the only thing that can force a newline NOT to be treated as semicolon is AFTER following symbols: ``` * / - + : ! . , ( { [ ``` They are like normal punctuation in a real sentence, if you put it you are going to end a sentence, which is natural, otherwise it is abrupt and doesn't makes sense. So a junk like int a = b *c in no way is equal to int a = b * c
Apr 01 2021
parent evilrat <evilrat666 gmail.com> writes:
On Thursday, 1 April 2021 at 19:32:17 UTC, evilrat wrote:
 e.g. the only thing that can force a newline NOT to be treated 
 as semicolon is AFTER following symbols:
 ```
 * / - + : !   . , ( { [
 ```
Ok, turns out life is a bit more complex than that and we have things like strings and functional paradigm. So in addition to the rule above it seems we might need overlapping rule BEFORE for symbols like . and (
Apr 01 2021
prev sibling parent rempas <rempas tutanota.com> writes:
On Wednesday, 31 March 2021 at 21:59:22 UTC, deadalnix wrote:
 On Wednesday, 31 March 2021 at 21:51:07 UTC, Arun wrote:
 Just curious. Does the compiler need semicolon anymore? Are 
 there corner cases that don't work without semicolon?
They aren't strictly needed most of the time, but they do make things much clearer, especially when you have a syntax error.
Since when? I use LDC 1.24 and it won't compile without semicolons...
May 13 2021
prev sibling next sibling parent Kagamin <spam here.lot> writes:
On Wednesday, 31 March 2021 at 21:51:07 UTC, Arun wrote:
 Just curious. Does the compiler need semicolon anymore? Are 
 there corner cases that don't work without semicolon?
See http://delight.sourceforge.net/
Apr 02 2021
prev sibling next sibling parent John Colvin <john.loughran.colvin gmail.com> writes:
On Wednesday, 31 March 2021 at 21:51:07 UTC, Arun wrote:
 Just curious. Does the compiler need semicolon anymore? Are 
 there corner cases that don't work without semicolon?
SIL (in-house language Symmetry Investments, implemented in D) has optional semi-colons. We made them work by insisting that statements are unambiguous between eager and non-eager parsing over line-breaks. Instead of choosing which version is correct with a rule, we simply disallow anything that would be ambiguous. For example: ``` a = foo (bar()) ``` Instead of deciding which interpretation is correct (assign the function `foo` to `a` then call `bar` *or* call `bar`, pass result to `foo`, assign result of `foo` to `a`), this is simply a parse error and the user must include a backslash or a semicolon after `foo` to disambiguate. We are more conservative about this than we should be and therefore disallow some code that isn’t actually ambiguous, but in practice it doesn’t seem to be much of a problem. Having said all that, D is fine with semicolons and should keep them.
Apr 03 2021
prev sibling parent reply Witold Baryluk <witold.baryluk gmail.com> writes:
On Wednesday, 31 March 2021 at 21:51:07 UTC, Arun wrote:
 Just curious. Does the compiler need semicolon anymore? Are 
 there corner cases that don't work without semicolon?
Please never even considering semicolons to be optional in D. I once hit a optional semicolon issue in JavaScript, that took two weeks to debug. I hate that feature since then (~2005). If you don't want to use semicolons, just use Python-like syntax, and then run it thour some converter for example.
May 11 2021
parent reply deadalnix <deadalnix gmail.com> writes:
On Tuesday, 11 May 2021 at 15:41:02 UTC, Witold Baryluk wrote:
 On Wednesday, 31 March 2021 at 21:51:07 UTC, Arun wrote:
 Just curious. Does the compiler need semicolon anymore? Are 
 there corner cases that don't work without semicolon?
Please never even considering semicolons to be optional in D. I once hit a optional semicolon issue in JavaScript, that took two weeks to debug. I hate that feature since then (~2005). If you don't want to use semicolons, just use Python-like syntax, and then run it thour some converter for example.
function foo() { return { foo: "bar" } } console.log(foo()) What does this log? "undefined", obviously, what did you expect? I learned this the hard way too.
May 11 2021
next sibling parent evilrat <evilrat666 gmail.com> writes:
On Tuesday, 11 May 2021 at 16:31:34 UTC, deadalnix wrote:
 On Tuesday, 11 May 2021 at 15:41:02 UTC, Witold Baryluk wrote:
 On Wednesday, 31 March 2021 at 21:51:07 UTC, Arun wrote:
 Just curious. Does the compiler need semicolon anymore? Are 
 there corner cases that don't work without semicolon?
Please never even considering semicolons to be optional in D. I once hit a optional semicolon issue in JavaScript, that took two weeks to debug. I hate that feature since then (~2005). If you don't want to use semicolons, just use Python-like syntax, and then run it thour some converter for example.
function foo() { return { foo: "bar" } } console.log(foo()) What does this log? "undefined", obviously, what did you expect? I learned this the hard way too.
Will not work unless it is `void foo()` function. LDC will also complain about unreachable code, at least in release/optimization mode. It is also another story in languages with implicit last statement return like Nim or Kotlin (and Rust IIRC).
May 11 2021
prev sibling parent Witold Baryluk <witold.baryluk gmail.com> writes:
On Tuesday, 11 May 2021 at 16:31:34 UTC, deadalnix wrote:
 On Tuesday, 11 May 2021 at 15:41:02 UTC, Witold Baryluk wrote:
 On Wednesday, 31 March 2021 at 21:51:07 UTC, Arun wrote:
 Just curious. Does the compiler need semicolon anymore? Are 
 there corner cases that don't work without semicolon?
Please never even considering semicolons to be optional in D. I once hit a optional semicolon issue in JavaScript, that took two weeks to debug. I hate that feature since then (~2005). If you don't want to use semicolons, just use Python-like syntax, and then run it thour some converter for example.
function foo() { return { foo: "bar" } } console.log(foo()) What does this log? "undefined", obviously, what did you expect? I learned this the hard way too.
Yeah. How about this: ```javascript function bar(x) { return x; } function baz(x) { return x; } function foo(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) { return bar(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) + baz(aaaaaaaaaaaaaaaaaaaaaaaaaaaa); } console.log(foo(21)) ``` Learned very hard way.
May 11 2021