variable-assignment

What's the rationale for preventing assignment to arrays?

帅比萌擦擦* 提交于 2019-11-28 06:57:23
问题 I've tried to google this and have read: Why can't arrays of same type and size be assigned? Assigning arrays Assign to array in struct in c But they all state the obvious: you can't assign to arrays because the standard says so. That's great and all, but I want to know why the standard doesn't include support for assigning to arrays. The standard committee discusses things in detail, and I'd be surprised if they never discussed making arrays assignable. Assuming they've discussed it, they

Direct Initialization vs Copy Initialization for Primitives

谁说胖子不能爱 提交于 2019-11-28 05:50:13
问题 When initializing primitive types like int or pointers one can use either copy-initialization or direct-initialization . int a = 10; int b(10); Although the latter way is preffered for objects with constructors, I don't see people using it for primitives. I understand that it is kind of "more natural" (especially for numbers) to use the '=' operator but is there anybody writing things like in real-life code: for (int i(0); i < 5; ++i) { cout << i << endl; } Thanks. EDIT: The question asks

What is the “pin” operator for, and are Elixir variables mutable?

被刻印的时光 ゝ 提交于 2019-11-28 05:15:37
Currently trying to understand the "^" operator in Elixir. From the website: The pin operator ^ can be used when there is no interest in rebinding a variable but rather in matching against its value prior to the match: Source - http://elixir-lang.org/getting_started/4.html With this in mind, you can attach a new value to a symbol like so: iex> x = 1 # Outputs "1" iex> x = 2 # Outputs "2" I can also do: iex> x = x + 1 # Outputs "3"! So my first question is; Are Elixir variables mutable? It sure looks like if that's the case... Shouldn't that be possible in a functional programming language? So

Why can I use the same name for iterator and sequence in a Python for loop?

房东的猫 提交于 2019-11-28 04:36:39
This is more of a conceptual question. I recently saw a piece of code in Python (it worked in 2.7, and it might also have been run in 2.5 as well) in which a for loop used the same name for both the list that was being iterated over and the item in the list, which strikes me as both bad practice and something that should not work at all. For example: x = [1,2,3,4,5] for x in x: print x print x Yields: 1 2 3 4 5 5 Now, it makes sense to me that the last value printed would be the last value assigned to x from the loop, but I fail to understand why you'd be able to use the same variable name for

Assign same value to multiple variables at once?

孤街醉人 提交于 2019-11-28 03:52:39
How can I assign the same value for multiple variables in PHP at once ? I have something like: $var_a = 'A'; $var_b = 'A'; $same_var = 'A'; $var_d = 'A'; $some_var ='A'; In my case, I can't rename all variables to have the same name (that would make things more easy), so is there any way to assign the same value to all variables in a much more compact way? $var_a = $var_b = $same_var = $var_d = $some_var = 'A'; 来源: https://stackoverflow.com/questions/11651594/assign-same-value-to-multiple-variables-at-once

Java - quick way of assigning array values to individual variables

三世轮回 提交于 2019-11-28 03:14:59
问题 I have a method which will return two strings in an array, split(str, ":", 2) to be precise. Is there a quicker way in java to assign the two values in the array to string variables than String[] strings = str.split(":", 2); String string1 = strings[0]; String string2 = strings[1]; For example is there a syntax like String<string1, string2> = str.split(":", 2); Thanks in advance. 回答1: No, there is no such syntax in Java. However, some other languages have such syntax. Examples include Python

Assigning an IronPython method to a C# delegate

做~自己de王妃 提交于 2019-11-28 01:12:04
问题 I have a C# class that looks a little like: public class MyClass { private Func<IDataCource, object> processMethod = (ds) => { //default method for the class } public Func<IDataCource, object> ProcessMethod { get{ return processMethod; } set{ processMethod = value; } } /* Other details elided */ } And I have an IronPython script that gets run in the application that looks like from MyApp import myObj #instance of MyClass def OtherMethod(ds): if ds.Data.Length > 0 : quot = sum(ds.Data.Real)

Javascript - Assigning multiple variables to object properties using curly braces in variable declaration

允我心安 提交于 2019-11-27 23:44:26
While looking at some Javascript code for Mozilla's (Firefox) Add-on SDK, I saw kind of variable declaration I hadn't seen before: var { foo, bar } = someFunction("whatever"); // just an example See those curly braces around the variable name? Turns out, this is a way of assigning the values of properties of an object to multiple variables all at once. It seems similar to destructuring assignment or PHP's list , except with object properties instead of arrays. I actually found this out through some fiddling, since there appears to be no documentation on it. Take a look at this code: function

Golang mixed assignation and declaration

两盒软妹~` 提交于 2019-11-27 23:36:15
I started working with go for a few weeks, and (once again) I stumbled across something that seems odd for me: // Not working a := 1 { a, b := 2, 3 } // Works a := 1 a, b := 2, 3 playground I want to assign two variables simultaneously. One is already declared, in a superior scope, the other one is not. It does not work: the compiler tries to redeclare the former variable. However, it works fine if this variable is declared in the same scope. Why is that ? What you're experiencing is commonly known as "variable shadowing" . When you use := with any variable in an inner scope, including in

Multiple Unpacking Assignment in Python when you don't know the sequence length

我怕爱的太早我们不能终老 提交于 2019-11-27 22:57:00
The textbook examples of multiple unpacking assignment are something like: import numpy as NP M = NP.arange(5) a, b, c, d, e = M # so of course, a = 0, b = 1, etc. M = NP.arange(20).reshape(5, 4) # numpy 5x4 array a, b, c, d, e = M # here, a = M[0,:], b = M[1,:], etc. (ie, a single row of M is assigned each to a through e) (My question is not numpy specific. Indeed, I would prefer a pure Python solution.) For the piece of code I'm looking at now, I see two complications on that straightforward scenario: I usually won't know the shape of M; and I want to unpack a certain number of items