mutability

How can I modify self in a closure called from a member function?

谁都会走 提交于 2019-11-28 07:21:20
问题 I am trying to calculate legal chess moves and am having problems satisfying the borrow checker. I have a struct Chess that implements these methods (non-important code replaced by ... ): // internal iterator over (possibly not legal) moves fn get_moves<F>(&self, func: F) where F: Fn(/* ... */), { func(/* ... */); // move 1 func(/* ... */); // move 2 func(/* ... */); // etc... } fn is_legal_move(&mut self) -> bool { // notice this takes a mutable self. For performance // reasons, the move is

Swift make method parameter mutable?

无人久伴 提交于 2019-11-28 04:26:38
How can I deal with this error without creating additional variable? func reduceToZero(x:Int) -> Int { while (x != 0) { x = x-1 // ERROR: cannot assign to 'let' value 'x' } return x } I don't want to create additional variable just to store the value of x. Is it even possible to do what I want? As stated in other answers, as of Swift 3 placing var before a variable has been deprecated. Though not stated in other answers is the ability to declare an inout parameter. Think: passing in a pointer. func reduceToZero(_ x: inout Int) { while (x != 0) { x = x-1 } } var a = 3 reduceToZero(&a) print(a)

Why it's impossible to override `var` with `def` in Scala?

北战南征 提交于 2019-11-28 02:01:47
While I understand why a var cannot override a val in subclass and vice versa, I am unable to understand why does Scala not allow a def in subclass to override a var in superclass class Car { var age = 32 } class SedanCar extends Car { override def age = 54 } As var is mutable why not allow a def to override it? Can anyone please help me in understanding this? dk14 That's related to the Liskov Substitution Principle : you can't assign weaker access privileges in subclass (even for Java) . Making var a def makes the setter def x_= (y: T ): Unit private (as @Staix said). So in case when Seadan

Is making in-place operations return the object a bad idea?

痴心易碎 提交于 2019-11-27 17:14:42
问题 I'm talking mostly about Python here, but I suppose this probably holds for most languages. If I have a mutable object, is it a bad idea to make an in-place operation also return the object? It seems like most examples just modify the object and return None . For example, list.sort . 回答1: Yes, it is a bad idea. The reason is that if in-place and non-in-place operations have apparently identical output, then programmers will frequently mix up in-place operations and non-in-place operations (

In Rust, what's the difference between “shadowing” and “mutability”?

不问归期 提交于 2019-11-27 15:16:59
In Chapter 3 of the Rust Book , Variables and Mutability , we go through a couple iterations on this theme in order to demonstrate the default, immutable behavior of variables in Rust: fn main() { let x = 5; println!("The value of x is {}", x); x = 6; println!("The value of x is {}", x); } Which outputs: error[E0384]: cannot assign twice to immutable variable `x` --> src/main.rs:4:5 | 2 | let x = 5; | - | | | first assignment to `x` | help: make this binding mutable: `mut x` 3 | println!("The value of x is {}", x); 4 | x = 6; | ^^^^^ cannot assign twice to immutable variable However, because

Swift make method parameter mutable?

陌路散爱 提交于 2019-11-27 05:20:43
问题 How can I deal with this error without creating additional variable? func reduceToZero(x:Int) -> Int { while (x != 0) { x = x-1 // ERROR: cannot assign to 'let' value 'x' } return x } I don't want to create additional variable just to store the value of x. Is it even possible to do what I want? 回答1: As stated in other answers, as of Swift 3 placing var before a variable has been deprecated. Though not stated in other answers is the ability to declare an inout parameter. Think: passing in a

cannot borrow `self.x` as immutable because `*self` is also borrowed as mutable

徘徊边缘 提交于 2019-11-27 05:16:13
First, let the code speak: #[derive(Debug)] struct Bar; #[derive(Debug)] struct Qux { baz: bool } #[derive(Debug)] struct Foo { bars: Vec<Bar>, qux: Qux, } impl Foo { fn get_qux(&mut self) -> &mut Qux { &mut self.qux } fn run(&mut self) { // 1. Fails: let mut qux = self.get_qux(); // 2. Works: // let mut qux = &mut Qux { baz: false }; // 3. Works: // let mut qux = &mut self.qux; let qux_mut = &mut qux; qux_mut.baz = true; for bar in &self.bars { println!("{:?}", bar); } } } fn main() { println!("Hello, world!"); let mut foo = Foo { bars: vec!(), qux: Qux { baz: false } }; foo.run(); } This

Why it's impossible to override `var` with `def` in Scala?

廉价感情. 提交于 2019-11-26 22:04:40
问题 While I understand why a var cannot override a val in subclass and vice versa, I am unable to understand why does Scala not allow a def in subclass to override a var in superclass class Car { var age = 32 } class SedanCar extends Car { override def age = 54 } As var is mutable why not allow a def to override it? Can anyone please help me in understanding this? 回答1: That's related to the Liskov Substitution Principle: you can't assign weaker access privileges in subclass (even for Java).

In Rust, what's the difference between “shadowing” and “mutability”?

霸气de小男生 提交于 2019-11-26 17:04:48
问题 In Chapter 3 of the Rust Book, Variables and Mutability , we go through a couple iterations on this theme in order to demonstrate the default, immutable behavior of variables in Rust: fn main() { let x = 5; println!("The value of x is {}", x); x = 6; println!("The value of x is {}", x); } Which outputs: error[E0384]: cannot assign twice to immutable variable `x` --> src/main.rs:4:5 | 2 | let x = 5; | - | | | first assignment to `x` | help: make this binding mutable: `mut x` 3 | println!("The

How do I return a reference to something inside a RefCell without breaking encapsulation?

北城余情 提交于 2019-11-26 14:43:00
I have a struct that has inner mutability. use std::cell::RefCell; struct MutableInterior { hide_me: i32, vec: Vec<i32>, } struct Foo { //although not used in this particular snippet, //the motivating problem uses interior mutability //via RefCell. interior: RefCell<MutableInterior>, } impl Foo { pub fn get_items(&self) -> &Vec<i32> { &self.interior.borrow().vec } } fn main() { let f = Foo { interior: RefCell::new(MutableInterior { vec: Vec::new(), hide_me: 2, }), }; let borrowed_f = &f; let items = borrowed_f.get_items(); } Produces the error: error[E0597]: borrowed value does not live long