digitalmars.D.learn - General rule when not to write ;
- Alain De Vos (3/3) May 18 2021 After each } i write a ;
- FeepingCreature (5/8) May 18 2021 Is `;` ever needed after a `}`?
- Jesse Phillips (13/16) May 19 2021 This is a good question, I'm not sure I can provide a concise
- Paul Backus (4/6) May 19 2021 An expression has a value. A statement doesn't.
- Alain De Vos (7/7) May 19 2021 It seems I need }; for a function a delegate and an alias.
- H. S. Teoh (13/19) May 19 2021 The ';' here is for terminating the alias, it is not part of the
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
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
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
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
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
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