mutable

Is the mutability of Swift containers shallow? Or can an element be mutated in place?

ε祈祈猫儿з 提交于 2020-01-05 12:58:27
问题 I have a collection of objects in a Set . The objects' type follows GeneratorType , so I have a mutating method next . Although I can mutate the set by adding/removing elements, I don't know how to mutate an element. Using both for-in statement and the forEach method give errors about the element being immutable. Is the mutability of Swift containers shallow? Is there a way to call a mutating method on a contained element? Does it work for other collection/sequence types besides Set ? (My

Is the mutability of Swift containers shallow? Or can an element be mutated in place?

余生颓废 提交于 2020-01-05 12:57:48
问题 I have a collection of objects in a Set . The objects' type follows GeneratorType , so I have a mutating method next . Although I can mutate the set by adding/removing elements, I don't know how to mutate an element. Using both for-in statement and the forEach method give errors about the element being immutable. Is the mutability of Swift containers shallow? Is there a way to call a mutating method on a contained element? Does it work for other collection/sequence types besides Set ? (My

Are there any good reasons why closures aren't immutable in C#?

≯℡__Kan透↙ 提交于 2019-12-31 16:46:44
问题 I've been going over and over this in my head, and I can't seem to come up with a good reason why C# closures are mutable. It just seems like a good way to get some unintended consequences if you aren't aware of exactly what's happening. Maybe someone who is a little more knowledgeable can shed some light on why the designers of C# would allow state to change in a closure? Example: var foo = "hello"; Action bar = () => Console.WriteLine(foo); bar(); foo = "goodbye"; bar(); This will print

pre-defined constants for non-trivial data types

空扰寡人 提交于 2019-12-30 07:15:10
问题 My Goal: Create a C# class for predefined errors that have both an ID and a Message. Here was what I tried: public class MyError { public static readonly MyError OK = new MyError(0, "OK"); public static readonly MyError Bad = new MyError(1, "Bad Stuff"); public MyError(int id, string message) { this.Id = id; this.Message = message; } public readonly int Id; public readonly string Message; } This compiles just fine and I am sure it would work just fine in practice. But I always like to follow

Always declare std::mutex as mutable in C++11?

痞子三分冷 提交于 2019-12-29 11:45:16
问题 After watching Herb Sutter's talk You Don't Know const and mutable, I wonder whether I should always define a mutex as mutable? If yes, I guess the same holds for any synchronized container (e.g., tbb::concurrent_queue )? Some background: In his talk, he stated that const == mutable == thread-safe, and std::mutex is per definition thread-safe. There is also related question about the talk, Does const mean thread-safe in C++11. Edit: Here, I found a related question (possibly a duplicate). It

The immutable object in python

↘锁芯ラ 提交于 2019-12-29 09:23:07
问题 I see a article about the immutable object. It says when: variable = immutable As assign the immutable to a variable. for example a = b # b is a immutable It says in this case a refers to a copy of b , not reference to b . If b is mutable , the a wiil be a reference to b so: a = 10 b = a a =20 print (b) #b still is 10 but in this case: a = 10 b = 10 a is b # return True print id(10) print id(a) print id(b) # id(a) == id(b) == id(10) if a is the copy of 10 , and b is also the copy of 10 , why

Why doesn't the compiler report an error when a variable not declared as mutable is modified?

谁说胖子不能爱 提交于 2019-12-29 08:33:08
问题 I installed Rust 1.13 and tried: fn main() { let x: u32; x = 10; // no error? } When I compiled this file there's some warnings, but there's no error. As I'm not declaring x as mut , shouldn't x = 10; cause an error? 回答1: What you have written is identical to: let x: u32 = 10; The compiler will not permit you to mutate it thereafter: let x: u32; x = 10; x = 0; // Error: re-assignment of immutable variable `x` Note that it is a compiler error if you try to use an uninitialized variable: let x:

The final word on NSStrings: Mutable and Immutable

随声附和 提交于 2019-12-29 06:19:02
问题 I've read in several books... and online... about immutable and mutable strings. They claim "immutable strings" can't be changed. (But they never define "change".) Which of these NSStrings could be changed without using NSMutableString? The string contains "catfish"... and I later try to change it to "cat". (Same letters, just shorter.) It contains "cat"... and I try to change it to "catfish". (Similar start... but just made longer.) I change "cat" into "CAT". (Same letters, but just the case

Good uses for mutable function argument default values?

时间秒杀一切 提交于 2019-12-28 04:59:09
问题 It is a common mistake in Python to set a mutable object as the default value of an argument in a function. Here's an example taken from this excellent write-up by David Goodger: >>> def bad_append(new_item, a_list=[]): a_list.append(new_item) return a_list >>> print bad_append('one') ['one'] >>> print bad_append('two') ['one', 'two'] The explanation why this happens is here. And now for my question: Is there a good use-case for this syntax? I mean, if everybody who encounters it makes the

Generating sublists using multiplication ( * ) unexpected behavior [duplicate]

拥有回忆 提交于 2019-12-27 11:27:09
问题 This question already has answers here : List of lists changes reflected across sublists unexpectedly (13 answers) Nested List Indices [duplicate] (2 answers) Closed 6 years ago . I'm sure this has been answered somewhere but I wasn't sure how to describe it. Let's say I want to create a list containing 3 empty lists, like so: lst = [[], [], []] I thought I was being all clever by doing this: lst = [[]] * 3 But I discovered, after debugging some weird behavior, that this caused an append