Do I Need a Trailing Semicolon Here

Do I need a trailing semicolon here?

It is not required, but you should put it, as a good practice.

That way, the day you need to add another instruction after this one, it'll work fine.


And here is the manual's page that answers your question : Instruction separation (quoting, emphasis mine) :

As in C or Perl, PHP requires instructions to be terminated with a semicolon at the end of each statement.

The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block.

The closing tag for the block will include the immediately trailing newline if one is present.

Do I need a trailing semicolon to disambiguate this code?

I’d write it like this instead:

def checkRadioButton(xml: DslBuilder): String => XmlTree = {
val inputs = top(xml).\\*(hasLocalNameX("input"));
(buttonValue: String) => { // <-- changed position of {
// code omitted
}
}

Rust - Is semicolon necessary here?

In general, the trailing semicolon determines the return value of the block. If you leave the semicolon out, the return value is the value of the last expression in the block. With the semicolon included, the return value is always Rust's unit value, the empty tuple () (except when the block contains an expression that does not return, in which case the return type is the "never" type !, which does not have any values).

In this particular case, there is no semantic difference. Assignment expressions return the unit value as well, i.e. (var = var + 1) == (). However, this is more or less a coincidence. You don't actually want to return any value in that statement, so including the semicolon makes your intention much clearer.

Do we need semicolon at the end?

The concept is known as JavaScript Semicolon Insertion or "Automatic Semicolon Insertion". This blog post: JavaScript Semicolon Insertion: Everything you need to know (archived from the original) outlines the concept well in an understandable manner using examples under the headings:

  • Where Semicolons are Allowed
  • Where Semicolons May be Omitted
  • The rules

It even digs into the official ECMAScript specification about the topic.

Are semicolons optional in Rust?

They're not optional. Semicolons modify the behaviour of an expression statement so it should be a conscious decision whether you use them or not for a line of code.

Almost everything in Rust is an expression. An expression is something that returns a value. If you put a semicolon you are suppressing the result of this expression, which in most cases is what you want.

On the other hand, this means that if you end your function with an expression without a semicolon, the result of this last expression will be returned. The same can be applied for a block in a match statement.

You can use expressions without semicolons anywhere else a value is expected.

For example:

let a = {
let inner = 2;
inner * inner
};

Here the expression inner * inner does not end with a semicolon, so its value is not suppressed. Since it is the last expression in the block, its value will be returned and assigned to a. If you put a semicolon on this same line, the value of inner * inner won't be returned.

In your specific case, not suppressing the value of your let statement doesn't make sense, and the compiler is rightly giving you an error for it. In fact, let is not an expression.

Python syntax trailing semicolon

It's purely a style thing. It's not going to hurt performance, and the worst weird edge case you'll run into is that this:

do_whatever();;
# two of these^

is a syntax error.

Why must I put a semicolon at the end of class declaration in C++?

The full syntax is, essentially,

class NAME { constituents } instances ;

where "constituents" is the sequence of class elements and methods, and "instances" is a comma-separated list of instances of the class (i.e., objects).

Example:

class FOO {
int bar;
int baz;
} waldo;

declares both the class FOO and an object waldo.

The instance sequence may be empty, in which case you would have just

class FOO {
int bar;
int baz;
};

You have to put the semicolon there so the compiler will know whether you declared any instances or not.

This is a C compatibility thing.

Will CSS 3 still allow omitting final semicolons?

Looks like there currently is an ambiguity in the spec.

You correctly pointed out that 2. Syntax Description section prescribes ending every rule with a semicolon:

Each declaration has a property name, followed by a colon and the property value, and finished with a semicolon.

And at the same time, description of the parsing automaton in section 3.6.8. Declaration-value mode reads that a closing brace without a semicolon correctly ends a declaration and current rule at the same time:

} token

Append the current declaration to the value of the current rule. Pop the current rule from the stack of open rules, and append it to the value of the new current rule. Switch to the current rule's content mode.

So according to 3.6.8 trailing semicolon is optional.

I can't say about actual intention of the specification authors. But current situation should probably be reported and fixed. Most certainly they don't have intention of breaking the compatibility with CSS 2.1 and will reword their Syntax Description section in the final version.

When is it appropriate to use a semicolon?

Just prefix lines starting with [, (, or ` with a semicolon and you're (almost) golden*

Using the same example as another answer:

var x = { xx : "hello", yy : "world"}
(function () {
console.log("Hello World");
})();

We add a semicolon according to this rule:

var x = { xx : "hello", yy : "world"}
;(function () {

otherwise javascript thinks we're trying to call( some function, or reference[ some array. This is simpler, easier to follow, and it's visually easier to spot. You also need semicolons in for loops, but the .forEach method is a cleaner and easier method. I'd confidently say this one rule covers 99% of the scenarios you need to use a semicolon in javascript/typescript.

Following this method, it's important to associate a newline with terminating a statement.

*This returns the venerable undefined:

  return 
7

After return, there's a newline, and the browser inserts a semicolon, terminating the statement like this:

  return; // this will return undefined.
7

Do this instead:

  return (
7
)

Javascript is actually pretty smart with semicolons, there's an open paren, so no semicolon is inserted until the closing paren is found.

If you have a habit of putting semicolons everywhere and not knowing exactly when they are needed, you could read this for a several page long explanation: http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding

I admit most people will still just litter semi colons at the end of every line, but if you're new and just learning, this is the better approach.

If you are using typescript (hopefully you are, it's 2023 now), and forget a semi, using my first example, you'll get this error:

lkasdljkasfldjk



Related Topics



Leave a reply



Submit