immutability

Why can immutable variables be passed as arguments to functions that require mutable arguments?

雨燕双飞 提交于 2020-05-15 06:19:08
问题 Example code: fn main() { let a = [1, 2, 3, 4, 5]; reset(a); } fn reset(mut b: [u32; 5]) { b[0] = 5; } The variable a is an immutable array, and the reset function's parameter b is a mutable array; intuitively I need to modify a to a mutable array before I can call the reset method, but the compiler tells me that I don't need to do this, why is this? fn main() { let mut a = [1, 2, 3, 4, 5]; reset(a); } fn reset(mut b: [u32; 5]) { b[0] = 5; } warning: variable does not need to be mutable -->

Kotlin: lateinit to val, or, alternatively, a var that can set once

♀尐吖头ヾ 提交于 2020-05-09 19:04:41
问题 Just curious: In Kotlin, I would love to get some val that can be initialized by lazy, but with a parameter. That's because I need something that's created very late in order to initialize it. Specifically, I wish I had: private lateinit val controlObj:SomeView or: private val controlObj:SomeView by lazy { view:View->view.findViewById(...)} and then: override fun onCreateView(....) { val view = inflate(....) controlObj = view.findViewById(...) or in the 2nd case controlObj.initWith(view) or

Java: pseudo-setter method for immutable classes

和自甴很熟 提交于 2020-04-12 07:48:09
问题 Let's say I have a class Foo in Java that has immutable data: class Foo { final private int x; public int getX() { return this.x; } final private OtherStuff otherstuff; public Foo(int x, OtherStuff otherstuff) { this.x = x; this.otherstuff = otherstuff; } // lots of other stuff... } Now I'd like to add a utility method that creates a "sibling" value with identical state but with a new value of x. I could call it setX() : class Foo { ... Foo setX(int newX) { return new Foo(newX, this

How to create Immutable List in java?

自古美人都是妖i 提交于 2020-04-05 11:13:12
问题 I need to convert mutable list object to immutable list. What is the possible way in java? public void action() { List<MutableClass> mutableList = Arrays.asList(new MutableClass("san", "UK", 21), new MutableClass("peter", "US", 34)); List<MutableClass> immutableList = immutateList(mutableList); } public List<MutableClass> immutateList(List<MutableClass> mutableList){ //some code here to make this beanList immutable //ie. objects and size of beanList should not be change. //ie. we cant add new

How to create Immutable List in java?

断了今生、忘了曾经 提交于 2020-04-05 11:11:27
问题 I need to convert mutable list object to immutable list. What is the possible way in java? public void action() { List<MutableClass> mutableList = Arrays.asList(new MutableClass("san", "UK", 21), new MutableClass("peter", "US", 34)); List<MutableClass> immutableList = immutateList(mutableList); } public List<MutableClass> immutateList(List<MutableClass> mutableList){ //some code here to make this beanList immutable //ie. objects and size of beanList should not be change. //ie. we cant add new

How to check immutability [duplicate]

﹥>﹥吖頭↗ 提交于 2020-03-18 11:55:05
问题 This question already has answers here : Check for mutability in Python? (6 answers) Closed 6 years ago . In python, is there any way to check if an object is immutable or mutable? like isimmutable(a) would return True , if a is immutable, else returns False . 回答1: There are no general tests for immutability. An object is immutable only if none of its methods can mutate the underlying data. take a look at this question the answer says: 1) Keys must not be mutable, unless you have a user

How to check immutability [duplicate]

断了今生、忘了曾经 提交于 2020-03-18 11:53:47
问题 This question already has answers here : Check for mutability in Python? (6 answers) Closed 6 years ago . In python, is there any way to check if an object is immutable or mutable? like isimmutable(a) would return True , if a is immutable, else returns False . 回答1: There are no general tests for immutability. An object is immutable only if none of its methods can mutate the underlying data. take a look at this question the answer says: 1) Keys must not be mutable, unless you have a user

Python - Why does extend() and append() return None (void)? [duplicate]

给你一囗甜甜゛ 提交于 2020-03-16 02:38:06
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: why does python's list.append evaluate to false? In my opinion, I think list1.extend(list2) and list1.append(num) should return the mutated list rather than None (void). As well as having the side effect of destructively mutating the original list. 回答1: I believe the intent was to promote readable code and reduce bugs. This decision was made a very long time ago, but you can probably find more by looking at the

Jackson deserialization circumventing final fields

删除回忆录丶 提交于 2020-02-23 09:07:12
问题 Here's the code import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.Data; import lombok.ToString; public class Main { public static void main(String[] args) throws Exception { Fields f1 = new Fields(1); System.out.println(f1); ObjectMapper mapper = new ObjectMapper(); String str = mapper.writeValueAsString(f1); System.out.println(str); Fields f2 = mapper.readValue(str, Fields.class); System.out.println(f2); } @Data @ToString

Jackson deserialization circumventing final fields

时光怂恿深爱的人放手 提交于 2020-02-23 09:07:08
问题 Here's the code import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.Data; import lombok.ToString; public class Main { public static void main(String[] args) throws Exception { Fields f1 = new Fields(1); System.out.println(f1); ObjectMapper mapper = new ObjectMapper(); String str = mapper.writeValueAsString(f1); System.out.println(str); Fields f2 = mapper.readValue(str, Fields.class); System.out.println(f2); } @Data @ToString