immutability

Scala immutable objects and traits with val fields

孤街醉人 提交于 2019-12-03 06:07:36
I would like to construct my domain model using immutable objects only. But I also want to use traits with val fields and move some functionality to traits. Please look at the following example: trait Versionable { val version = 0 def incrementVersion = copy(version=version+1) } Unfortunatelly such code doesn't work - copy method is unknown for trait Versionable. I think that it would be nice to have copy method generated for every trait and class. Such method should create shallow copy of object and return it using the same type as for original object with given field modified accoring to

When to use mutable vs immutable classes in Scala

跟風遠走 提交于 2019-12-03 06:07:18
Much is written about the advantages of immutable state, but are there common cases in Scala where it makes sense to prefer mutable classes? (This is a Scala newbie question from someone with a background in "classic" OOP design using mutable classes.) For something trivial like a 3-dimensional Point class, I get the advantages of immutability. But what about something like a Motor class, which exposes a variety of control variables and/or sensor readings? Would a seasoned Scala developer typically write such a class to be immutable? In that case, would 'speed' be represented internally as a

Strings vs classes when both are reference types

↘锁芯ラ 提交于 2019-12-03 06:01:04
问题 Here is how my last interview went: Question: Where are strings stored? Answer: Heap since it is a reference type Question: Explain me this following code: static void Main(string[] args) { string one = "test"; string two = one; one = one + " string"; Console.WriteLine("One is {0}" , one); Console.WriteLine("Two is {0}", two); } Answer: Drew two diagrams like below: (represents the statement, string two = one; (represents the statement, one = one + " string"; . A new string is created on heap

non-technical benefits of having string-type immutable

自作多情 提交于 2019-12-03 05:53:00
问题 I am wondering about the benefits of having the string-type immutable from the programmers point-of-view. Technical benefits (on the compiler/language side) can be summarized mostly that it is easier to do optimisations if the type is immutable. Read here for a related question. Also, in a mutable string type, either you have thread-safety already built-in (then again, optimisations are harder to do) or you have to do it yourself. You will have in any case the choice to use a mutable string

Immutable functional objects in highly mutable domain

强颜欢笑 提交于 2019-12-03 05:40:20
问题 I'm currently learning functional programming in my spare time with Scala, and I have an idle newbie question. I can see the elegance of having immutable objects when doing something like calculating a Haar wavelet transform - i.e. when the data itself being represented by the objects doesn't change. But I saw a blog where someone had a small game as an example when demonstrating immutability. If a creature object recieved damage, it didn't change its state - it returned a new creature object

C# immutable int

跟風遠走 提交于 2019-12-03 05:16:18
In Java strings are immutable. If we have a string and make changes to it, we get new string referenced by the same variable: String str = "abc"; str += "def"; // now str refers to another piece in the heap containing "abcdef" // while "abc" is still somewhere in the heap until taken by GC It's been said that int and double are immutable in C#. Does it mean that when we have int and later change it, we would get new int "pointed" by the same variable? Same thing but with stack. int i = 1; i += 1; // same thing: in the stack there is value 2 to which variable // i is attached, and somewhere in

Complex data structures in Haskell - how do they work?

冷暖自知 提交于 2019-12-03 05:06:17
问题 As I figured out, variables in Haskell are immutable (thus, they are not really `variables'). In this case, if we have a complex and big data structure, like a red-black tree, how are we supposed to implement operations that actually change the data structure? Create a copy of the tree each time an element is inserted or deleted? 回答1: Yes, the solution is to return a new data structure which represents the modified value, however there is no requirement to copy the entire structure for your

Jackson JSON, Immutable Classes, and Interfaces

梦想与她 提交于 2019-12-03 05:05:10
I am playing with the Jackson examples and am having some trouble getting deserialization to work with immutable classes and interfaces. Below is my code: package com.art.starter.jackson_starter; import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; /** * Hello world! * */ public class App { public static void main( String[] args ) throws JsonGenerationException, JsonMappingException, IOException { System.out

Is it possible to have immutable fields in Hibernate/JPA?

天大地大妈咪最大 提交于 2019-12-03 04:59:19
In our application, we need to have fields that are assignable only once. At first we thought of encapsulating the fields and making the setters private. However, some questions arouse: Without a public setter, is Hibernate still able to map the field from the database? Can I strip out the setter and make the field mutable only in the entity constructor? Finally, is there any standard JPA way to make a field immutable? Thanks in advance. Ad. 1: I believe JPA uses plain private fields for both read and write if annotations are placed on fields and not on getters. Recently I discovered that

Effectively Immutable Object

坚强是说给别人听的谎言 提交于 2019-12-03 04:34:52
问题 I want to make sure that I correctly understand the 'Effectively Immutable Objects' behavior according to Java Memory Model. Let's say we have a mutable class which we want to publish as an effectively immutable: class Outworld { // This MAY be accessed by multiple threads public static volatile MutableLong published; } // This class is mutable class MutableLong { private long value; public MutableLong(long value) { this.value = value; } public void increment() { value++; } public long get()