www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - General rule when not to write ;

reply Alain De Vos <devosalain ymail.com> writes:
After each } i write a ;
And let the compiler tell me it is an empty instruction.
What are the general rules where ; is not needed after a }
May 18 2021
next sibling parent FeepingCreature <feepingcreature gmail.com> writes:
On Tuesday, 18 May 2021 at 16:27:13 UTC, Alain De Vos wrote:
 After each } i write a ;
 And let the compiler tell me it is an empty instruction.
 What are the general rules where ; is not needed after a }
Is `;` ever needed after a `}`? I guess in `void delegate() dg = { writeln!"Hello World"; };`, but that hardly counts, because it belongs to the variable declaration, not the `{}`.
May 18 2021
prev sibling parent reply Jesse Phillips <Jesse.K.Phillips+D gmail.com> writes:
On Tuesday, 18 May 2021 at 16:27:13 UTC, Alain De Vos wrote:
 After each } i write a ;
 And let the compiler tell me it is an empty instruction.
 What are the general rules where ; is not needed after a }
This is a good question, I'm not sure I can provide a concise answer. In general you don't need a ; after } The ; is used to end a statement, but I don't know how to define that and distinguish it from an expression. The {} create a block of code, usually they will contain statements, so it would be common to see ; inside. The only real time I would expect a }; is when you're defining a lambda/delegate and assigning to a variable. auto Foo = X; If X is something that ends with } we will still expect a ; to end the statement.
May 19 2021
parent reply Paul Backus <snarwin gmail.com> writes:
On Wednesday, 19 May 2021 at 13:46:55 UTC, Jesse Phillips wrote:
 The ; is used to end a statement, but I don't know how to 
 define that and distinguish it from an expression.
An expression has a value. A statement doesn't. You can add a `;` at the end of an expression to make a statement from it. When you do, the value of that expression is discarded.
May 19 2021
parent reply Alain De Vos <devosalain ymail.com> writes:
It seems I need }; for a function a delegate and an alias.
```
double function(int) F = function double(int x) {return x/10.0;};
double delegate(int)   D = delegate double(int x) {return 
c*x/10.0;};
alias myfunx=function int(int number) { return number; };
```
May 19 2021
parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, May 19, 2021 at 05:53:12PM +0000, Alain De Vos via Digitalmars-d-learn
wrote:
 It seems I need }; for a function a delegate and an alias.
 ```
 double function(int) F = function double(int x) {return x/10.0;};
 double delegate(int)   D = delegate double(int x) {return c*x/10.0;};
 alias myfunx=function int(int number) { return number; };
 ```
The ';' here is for terminating the alias, it is not part of the delegate. Basically, the grammar is this: alias SYMBOL = DEFINITION ; It just so happens that DEFINITION here is a function literal: delegate ReturnType(...) { ... } If you substitute this into the grammar, you get: alias SYMBOL = delegate ReturnType(...) { ... } ; That's all there is to it. This isn't rocket science. T -- Don't drink and derive. Alcohol and algebra don't mix.
May 19 2021