mutability

How to implement a trait for any mutability?

你离开我真会死。 提交于 2019-12-05 07:06:48
Can mutability be a generic parameter in traits? I'd like to implement a trait for a mutable and an immutable variant of a type without having to copy&paste the impl block. trait Foo<T> {…} impl<T> Foo for *const T {…} impl<T> Foo for *mut T {…same thing again…} Wishful pseudocode: trait Foo<T> {…} impl<T, Mutability> Foo for *Mutability T {…} Can mutability be a generic parameter in traits? No. ^_^ Here's some detailed discussion on the matter ( Internals , Reddit ). I think in general people recognize that the current state is not ideal, but that it's not terribly constraining at the moment,

Command Pattern vs. Visitor Pattern

ぐ巨炮叔叔 提交于 2019-12-05 04:27:31
Is it generally acceptable to allow a Visitor to modify state of the Receiver, or should that be a Command pattern instead? The purpose of the visitor pattern is to allow new operations to be added to a class heirarchy without modification to that heirarchy. I've never seen anyone suggesting that only read-only operations are acceptable. The only limitation is that the added operations should only use the public interface of the class heirarchy. I don't think you can make a blanket statement whether it's good or bad to modify state of anything. I would think it's ok to modify the states as

Publishing Non-Thread Safe Object Fields in a Thread-Safe Manner

别等时光非礼了梦想. 提交于 2019-12-04 23:04:25
问题 I've got a problem with Java concurrency. Yes, I looked at questions with almost the exact same title, but they all seemed to be asking subtly different things. Yes, I've read Java Concurrency in Practice . Yes, I can see why it's the defacto reference for the topic. Yes, I've read the section specifically on publishing fields in thread-safe classes. Yes, I'm still going to ask a concurrency question on Java regardless of the fact that I know someone will simply point me to that book. This

Publishing Non-Thread Safe Object Fields in a Thread-Safe Manner

烂漫一生 提交于 2019-12-03 13:39:34
I've got a problem with Java concurrency. Yes, I looked at questions with almost the exact same title, but they all seemed to be asking subtly different things. Yes, I've read Java Concurrency in Practice . Yes, I can see why it's the defacto reference for the topic. Yes, I've read the section specifically on publishing fields in thread-safe classes. Yes, I'm still going to ask a concurrency question on Java regardless of the fact that I know someone will simply point me to that book. This has me stumped though -- I know that you can easily publish mutable primitive fields in a thread-safe

NSMutableArray can't be added to

こ雲淡風輕ζ 提交于 2019-12-02 02:52:29
I've had this sort of problem before, and it didn't get a satisfactory answer. I have a viewcontroller with a property called "counties" that is an NSMutableArray. I'm going to drill down a navigation screen to a view that is about selecting the counties for a geographical search. So the search page drills down to the "select counties" page. I pass NSMutableArray *counties to the second controller as I push the second one on the navigation stack. I actually set that second controller's "selectedCounties" property (also an NSMutableArray) with a pointer to my first controller's "counties", as

How to use struct self in member method closure

我是研究僧i 提交于 2019-12-02 02:48:48
问题 How can I call a method in closure? get_access_token method can set new access token based on self.get_base_url() : fn fetch_access_token(_base_url: &String) -> String { String::new() } fn get_env_url() -> String { String::new() } pub struct App { pub base_url: Option<String>, pub access_token: Option<String>, } impl App { pub fn new() -> App { App { base_url: None, access_token: None, } } pub fn get_base_url(&mut self) -> &String { self.base_url.get_or_insert_with(|| get_env_url()) } pub fn

Is it possible to mutate a string in Swift such that it can be proven to modify the original value?

亡梦爱人 提交于 2019-12-02 02:41:45
问题 Start with the claim that Swift makes that strings are 'mutable', Are Swift "mutable" strings really mutable, or are they just like Java strings?, but proceeding with a generally-accepted (and non-Swift ) definition of mutability - ie. strictly value immutability, without consideration of bindings Is it possible to actually mutate a String value such that this prints 'true'? var str = "Mutate me!" let a1 = (unsafeAddressOf(str)) // some 'mutating operation' let a2 = (unsafeAddressOf(str))

Differences in Object modifications

时间秒杀一切 提交于 2019-12-02 02:02:53
问题 i was just wondering if anybody could help me out with this : StringBuilder s=new StringBuilder("0123456789"); s.substring(1, 2); System.out.println(s); s.delete(2, 8); System.out.println(s); the first Sysout gives 0123456789(although i expected a substring) but other Sysout gives 0189. I have noticed that also with some Time and Date classes.How can i figure out, when what form is going to modify original object (in this case s). Is this related to Mutability of objects? Is there any general

Is it possible to mutate a string in Swift such that it can be proven to modify the original value?

丶灬走出姿态 提交于 2019-12-02 01:56:33
Start with the claim that Swift makes that strings are 'mutable', Are Swift "mutable" strings really mutable, or are they just like Java strings? , but proceeding with a generally-accepted (and non-Swift ) definition of mutability - ie. strictly value immutability, without consideration of bindings Is it possible to actually mutate a String value such that this prints 'true'? var str = "Mutate me!" let a1 = (unsafeAddressOf(str)) // some 'mutating operation' let a2 = (unsafeAddressOf(str)) print(a1 == a2) I am not interested in 'structure types' or delayed copy semantics. This question is

How to use struct self in member method closure

大兔子大兔子 提交于 2019-12-02 01:01:57
How can I call a method in closure? get_access_token method can set new access token based on self.get_base_url() : fn fetch_access_token(_base_url: &String) -> String { String::new() } fn get_env_url() -> String { String::new() } pub struct App { pub base_url: Option<String>, pub access_token: Option<String>, } impl App { pub fn new() -> App { App { base_url: None, access_token: None, } } pub fn get_base_url(&mut self) -> &String { self.base_url.get_or_insert_with(|| get_env_url()) } pub fn get_access_token(&mut self) -> &String { self.access_token .get_or_insert_with(|| fetch_access_token