immutability

Kotlin make constructor of data class accept both List and MutableList but store a mutable instance of them

拜拜、爱过 提交于 2020-08-10 05:04:51
问题 I want to make a data class which can accept both list and mutable-list and if the list is instance of MutableList then directly make it a property else if it is a List then convert it into a MutableList and then store it. data class SidebarCategory(val title: String, val groups: MutableList<SidebarGroup>) { constructor(title: String, groups: List<SidebarGroup>) : this(title, if (groups is MutableList<SidebarGroup>) groups else groups.toMutableList()) } In the above code Platform declaration

Kotlin make constructor of data class accept both List and MutableList but store a mutable instance of them

匆匆过客 提交于 2020-08-10 05:04:29
问题 I want to make a data class which can accept both list and mutable-list and if the list is instance of MutableList then directly make it a property else if it is a List then convert it into a MutableList and then store it. data class SidebarCategory(val title: String, val groups: MutableList<SidebarGroup>) { constructor(title: String, groups: List<SidebarGroup>) : this(title, if (groups is MutableList<SidebarGroup>) groups else groups.toMutableList()) } In the above code Platform declaration

Why does a File need to be mutable to call Read::read_to_string?

*爱你&永不变心* 提交于 2020-08-06 07:50:28
问题 Here's a line from the 2nd edition Rust tutorial: let mut f = File::open(filename).expect("file not found"); I'm of the assumption that the file descriptor is a wrapper around a number that basically doesn't change and is read-only. The compiler complains that the file cannot be borrowed mutably, and I'm assuming it's because the method read_to_string takes the instance as the self argument as mutable, but the question is "why" ? What is ever going to change about the file descriptor? Is it

Lombok + Jackson immutables

空扰寡人 提交于 2020-07-31 07:33:45
问题 After updating my project to Spring Boot 1.5.10 Lombok stopped working correctly with Jackson. I mean immutable DTOs creation, when field names in my objects are not same as fields in json request: @Value @Builder public class MyImmutableDto implements Serializable { @JsonProperty("other-field-1-name") private final BigDecimal myField1; @JsonProperty("other-field-2-name") private final String myField2; and a lot of fields there... } So, after updating Spring Boot to 1.5.10 this code isn't

Mutable class as a child of an immutable class

大城市里の小女人 提交于 2020-07-05 02:35:33
问题 I want to have immutable Java objects like this (strongly simplified): class Immutable { protected String name; public Immutable(String name) { this.name = name; } public String getName() { return name; } } In some cases the object should not only be readable but mutable, so I could add mutability through inheritance: public class Mutable extends Immutable { public Mutable(String name) { super(name); } public void setName(String name) { super.name = name; } } While this is technically fine, I

Immutability in Python [duplicate]

余生长醉 提交于 2020-07-04 13:02:43
问题 This question already has answers here : Python strings are not immutable? [duplicate] (2 answers) Closed 3 days ago . I trying to understand how the immutability works in python. Since string are immutable in python, I was expecting the id to change every time I perform a string operation but it doesn't work as expected. example: The last operation on t doesn't change its id. Any ideas why? 回答1: I had a row of apples in different cells [memory containing variables (I will not go to the bit

How to create an immutable list in Kotlin that is also an immutable list in Java?

点点圈 提交于 2020-07-03 01:59:48
问题 I have a Java/Kotlin interop problem. A Kotlin immutable list is compiled into a normal java.util.ArrayList that is mutable! Kotlin (library): class A { val items: List<Item> = ArrayList() } Java (consumer): A a = new A(); a.getItems().add(new Item()); // Compiles and runs but I wish to fail or throw How to make my Kotlin class fully immutable from Java perspective too? 回答1: All non- Mutable____ collections in Kotlin are compile time read-only types by default, but not immutable . See the

How to create an immutable list in Kotlin that is also an immutable list in Java?

五迷三道 提交于 2020-07-03 01:59:32
问题 I have a Java/Kotlin interop problem. A Kotlin immutable list is compiled into a normal java.util.ArrayList that is mutable! Kotlin (library): class A { val items: List<Item> = ArrayList() } Java (consumer): A a = new A(); a.getItems().add(new Item()); // Compiles and runs but I wish to fail or throw How to make my Kotlin class fully immutable from Java perspective too? 回答1: All non- Mutable____ collections in Kotlin are compile time read-only types by default, but not immutable . See the