variable-assignment

<?= operator C++ greater less question mark equals sign

空扰寡人 提交于 2019-12-05 02:58:00
I saw <?= and >?= used in a code: http://community.topcoder.com/stat?c=problem_solution&rm=151152&rd=5854&pm=2923&cr=310333 I tried to compile without the includes to test if it's standard, but it didn't work. I then added the includes, but it still gives the same error: question-mark.cpp:15:5: error: expected primary-expression before ‘?’ token question-mark.cpp:15:6: error: expected primary-expression before ‘=’ token question-mark.cpp:15:9: error: expected ‘:’ before ‘;’ token question-mark.cpp:15:9: error: expected primary-expression before ‘;’ token #include <stdio.h> #include <algorithm>

Use of Colon in object assignment destructuring Javascript

南笙酒味 提交于 2019-12-05 01:11:05
问题 Working with React.js and React Router import React, { Component } from 'react'; const PrivateRoute = ({ component: Component, ...rest }) => ( <Route {...rest} render={} /> ) *{ component: Component, ...rest }* ..rest is the use of spread syntax but what does *component: Component* do 回答1: In ES6 this will assign the value to a new variable in this case named foo let obj = { name: 'Some Name', age: '42', gender: 'coder' }; let { name: foo, ...rest } = obj; console.log({foo, rest}) // { foo:

Multiassignment in VB like in C-Style languages

巧了我就是萌 提交于 2019-12-05 00:19:54
Is there a way to perform this in VB.NET like in the C-Style languages: struct Thickness { double _Left; double _Right; double _Top; double _Bottom; public Thickness(double uniformLength) { this._Left = this._Right = this._Top = this._Bottom = uniformLength; } } Expanding on Mark's correct answer This type of assignment style is not possible in VB.Net. The C# version of the code works because in C# assignment is an expression which produces a value. This is why it can be chained in this manner. In VB.Net assignment is a statement and not an expression. It produces no value and cannot be

Assignment statement value

心已入冬 提交于 2019-12-04 22:54:06
Everybody knows that in Python assignments do not return a value, presumably to avoid assignments on if statements when usually just a comparison is intended: >>> if a = b: File "<stdin>", line 1 if a = b: ^ SyntaxError: invalid syntax >>> if a == b: ... pass ... For the same reason, one could suspect that multiple assignments on the same statement were also syntax errors. In fact, a = (b = 2) is not a valid expression: >>> a = (b = 2) File "<stdin>", line 1 a = (b = 2) ^ SyntaxError: invalid syntax So, my question is: why a = b = 2 works in Python as it works in other languages where

How to catch a particular name assignment?

▼魔方 西西 提交于 2019-12-04 22:24:32
(Based on this question ): One can override the __setattr__ magic method for an object to have additional instructions when an attribute of an object is set. As in: class MyClass(object): def __init__(self, attribute=None): object.__init__(self) self.attribute = attribute def __setattr__(self, name, value): self.__dict__[name] = value if name == 'attribute': print("attribute's value is modified to {}.".format( self.attribute)) if __name__ == '__main__': my_obj = MyClass(True) while True: my_obj.attribute = input() How can I catch a particular name assignment in the current script without using

Partially initializing a C struct

ⅰ亾dé卋堺 提交于 2019-12-04 22:18:58
This link states that "When an automatic array or structure has a partial initializer, the remainder is initialized to 0". I decided to try out what I read and wrote the following piece of code: #include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { //int arr[3] = {2}; // line no. 7 struct s { int si; int sj; }; struct s myStruct; myStruct.si = 9; printf("%d\n", myStruct.sj); } I don't understand why 4096 (which I believe is some "garbage" value) is printed when I comment out line no. 7 and I get 0 when I uncomment line no. 7 . I don't think the arr declaration has got

Operator precedence of unary operators

我的未来我决定 提交于 2019-12-04 22:12:32
问题 Some information source on operator precedence like this says that unary operators like ! , ~ , + , - have higher precedence than assignment = . However, the following expressions are possible: !a = true # => false (with warning) a # => true ~a = 1 # => -2 a # => 1 +a = 1 # => 1 a # => 1 -a = 1 # => -1 a # => 1 Considering these results, the only possible explanation I can think of is that these unary operator have lower precedence than the assignment. If that is the case, then it would mean

Passing data from Controller to View in a PHP MVC app

≯℡__Kan透↙ 提交于 2019-12-04 20:14:58
问题 In almost all tutorials or answers on SO, I see a common way to send data from a Controller to the View, the class View often looks something similar than the code below: class View { protected $_file; protected $_data = array(); public function __construct($file) { $this->_file = $file; } public function set($key, $value) { $this->_data[$key] = $value; } public function get($key) { return $this->_data[$key]; } public function output() { if (!file_exists($this->_file)) { throw new Exception(

Java assignment issues - Is this atomic?

别来无恙 提交于 2019-12-04 18:55:09
问题 I've got some questions about Java's assigment. Strings I've got a class: public class Test { private String s; public synchronized void setS(String str){ s = s + " - " + str; } public String getS(){ return s; } } I'm using "synchronized" in my setter, and avoiding it in my getter, because in my app, there are a tons of data gettings, and very few settings. Settings must be synchronized to avoid inconsistency. My question is: is getting and setting a variable atomic? I mean, in a

Shorthand way for assigning object properties in Groovy?

匆匆过客 提交于 2019-12-04 17:38:24
问题 I create Groovy objects using this convention... Item item1 = new Item( name: "foo", weight: "150") ...is there a shorthand convention for manipulating properties object? something like this... item1( name: "hello", weight: "175") //this does not work, btw ;-) ...instead of... item1.name = "hello" item1.weight = "175" 回答1: You have the with method, as described by the great Mr Haki item1.with{ name = "hello" weight = "175" } 回答2: Yes, you can do it like this: item1.metaClass.setProperties