What Is "X && Foo()"

What is x && foo() ?

Both AND and OR operators can shortcut.

So && only tries the second expression if the first is true (truth-like, more specifically). The fact that the second operation does stuff (whatever the contents of foo() does) doesn't matter because it's not executed unless that first expression evaluates to something truthy. If it is truthy, it then will be executed in order to try the second test.

Conversely, if the first expression in an || statement is true, the second doesn't get touched. This is done because the whole statement can already be evaluated, the statement will result in true regardless of the outcome of the second expression, so it will be ignored and remain unexecuted.

The cases to watch out for when using shortcuts like this, of course, are the cases with operators where defined variables still evaluate to falsy values (e.g. 0), and truthy ones (e.g. 'zero').

x && x.foo() && x.bar() syntax in javascript - what does it mean

It is a shorthand for the following:

if (y && y.someProp) {
y.someFunction() // Call someFunction
}

So if y has a value that is not falsy and y.someProp is not falsy then it calls the someFunction function. It is a shorthand that some prefer and others don't.

What does (myVar && foo()) mean in JavaScript?

The expression evaluates to myvar if myvar is falsey, and foo() if myvar is truthy. The following snippets are nearly identical.

var x = (myvar && foo());

if(myvar){ var x = foo(); } else { var x = myvar; }

Does Typescript support the ?. operator? (And, what's it called?)

Yes. As of TypeScript 3.7 (released on November 5, 2019), this feature is supported and is called Optional Chaining:

At its core, optional chaining lets us write code where TypeScript can immediately stop running some expressions if we run into a null or undefined. The star of the show in optional chaining is the new ?. operator for optional property accesses.

Refer to the TypeScript 3.7 release notes for more details.


Prior to version 3.7, this was not supported in TypeScript, although it was requested as early as Issue #16 on the TypeScript repo (dating back to 2014).

As far as what to call this operator, there doesn't appear to be a consensus. In addition to "optional chaining" (which is also what it's called in JavaScript and Swift), there are a couple of other examples:

  • CoffeeScript refers to it as the existential operator (specifically, the "accessor variant" of the existential operator):

The accessor variant of the existential operator ?. can be used to soak up null references in a chain of properties. Use it instead of the dot accessor . in cases where the base value may be null or undefined.

  • C# calls this a null-conditional operator.

a null-conditional operator applies a member access, ?., or element access, ?[], operation to its operand only if that operand evaluates to non-null; otherwise, it returns null.

  • Kotlin refers to it as the safe call operator.

There are probably lots of other examples, too.

Difference between if (x) { foo(); } and x ? foo() : 0;

Generally, if is a statement (e.g. you can't assign it to a variable) and the ternary operator ?: is part of an expression (yields a value which can be assigned to variables).

Also, the if block can contain statements while the components of ?: can only contain expressions.

In the example you give, there is no difference, since you don't use the result of the ?:.
Both snippets evaluate foo() only if x is a true value.

C++ what happens first - destruction of function's parameters or handling of function's return values?

With this signature:

int foo(thing x) {
return 5;
}

You are creating a function where the instance of thing is a parameter. Parameters are passed in by the caller of the method, that mean that the expression here:

newThing = foo(bar);

creates a temporary object of thing which is copied from bar. Those temporaries have a lifetime upto the end of the full expression, therefore the destructor of this temporary is called after the assignment happened.

Why is that so? Because the compiler only generates one function called foo and this function can not construct x locally - it would not know with which parameters to construct it. There could be multiple constructors which could be valid and several different sets of parameters. The compiler therefore must construct the temporary on the caller's side of the call.

Why do anonymous objects sometimes require a default constructor?

You have declared a variable x with type Foo

struct Foo {
Foo(){}
Foo (std::string x) { std::cout << x << std::endl; }
void test(){ std::cout << "test" << std::endl; };
};

std::string x("hello, world");

int main () { Foo(x); x.test(); }

print "test"


What you want is use uniform initialization syntax Foo{x}

struct Foo {
Foo (std::string x) { std::cout << x << std::endl; }
};

std::string x("hello, world");

int main () { Foo{x}; }

print "hello, world"



Related Topics



Leave a reply



Submit