immutability

How to deal with the immutability of returned structs?

拈花ヽ惹草 提交于 2019-12-04 08:37:35
问题 I'm writing a game that has a huge 2D array of "cells". A cell takes only 3 bytes. I also have a class called CellMap, which contains the 2D array as a private field, and provides access to it via a public indexer. Profiling showed that a performance problem is caused by garbage collection of too many Cell objects. So I decided to make Cell a struct (it was a class). But now code like this doesn't work: cellMap[x, y].Population++; I can think of many options, but I don't really like any of

C#, immutability and public readonly fields

 ̄綄美尐妖づ 提交于 2019-12-04 08:15:21
问题 I have read in many places that exposing fields publicly is not a good idea, because if you later want to change to properties, you will have to recompile all the code which uses your class. However, in the case of immutable classes, I don't see why you would ever need to change to properties - you're not going to be adding logic to the 'set' after all. Any thoughts on this, am I missing something? Example of the difference, for those who read code more easily than text :) //Immutable Tuple

Parsing nested Records in Immutable.js

家住魔仙堡 提交于 2019-12-04 08:06:55
问题 Suppose I have the following Records defined using Immutable.js: var Address = Immutable.Record({street: '', city: '', zip: ''}); var User = Immutable.Record({name: '', address: new Address()}); How do I convert plain javascript object into the User record? I tried the following but it does not produce the expected output: var user = new User({name: 'Foo', address: {street: 'Bar', city: 'Baz'}}); // => Record { "name": "Foo", "address": [object Object] } I am aware that it is possible to

Slow performance from ImmutableList<T> Remove method in Microsoft.Bcl.Immutable

夙愿已清 提交于 2019-12-04 08:05:49
Experiencing some unexpected performance from Microsoft ImmutableList from NuGet package Microsoft.Bcl.Immutable version 1.0.34 and also 1.1.22-beta When removing items from the immutable list the performance is very slow. For an ImmutableList containing 20000 integer values (1...20000) if starting to remove from value 20000 to 1 it takes about 52 seconds to remove all items from the list. If I do the same with a generic List<T> where I create a copy of the list after each remove operation it takes about 500 ms. I was a bit surprised by these results since I thought the ImmutableList would be

Java Immutable Objects [closed]

丶灬走出姿态 提交于 2019-12-04 05:55:18
Closed . This question needs to be more focused. It is not currently accepting answers. Learn more . Want to improve this question? Update the question so it focuses on one problem only by editing this post . Closed 5 years ago . I am learning the concept of immutability. I understand that immutable objects cannot change their values once the object is created. But I didn't understand the following uses of immutable objects. They are are automatically thread-safe and have no synchronization issues. How ? Proof ? do not need a copy constructor. How ? Any example ? do not need an implementation

.NET ORMs, immutable value objects, structs, default constructors, and readonly properties

别来无恙 提交于 2019-12-04 05:11:53
I am just getting started with .NET ORMs, to the point where I haven't even decided between Entity Framework and NHibernate. But in both cases, I'm running into a problem in that they seem to want me to compromise the integrity of my domain model in various ways, especially on finer points of C# object design. This is one of several questions on the subject. I am very used to enforcing immutability on appropriate properties with a pattern that looks like this: public class Foo { private readonly string bar; public string Bar { return this.bar; } public Foo(string bar) { this.bar = bar; } }

Scala val has to be guarded with synchronized for concurrent access?

不问归期 提交于 2019-12-04 03:56:25
As I read, Scala immutable val doesn't get translated to Java final for various reasons. Does this mean that accessing a val from an other Thread must be guarded with synchronization in order to guarantee visibility? As object members, once initialized, val s never change their values during the lifetime of the object. As such, their values are guaranteed to be visible to all threads provided that the reference to the object didn't escape in the constructor. And, in fact, they get Java final modifiers as illustrated below: object Obj { val r = 1 def foo { val a = 1 def bar = a bar } } Using

Is String get/set threadsafe?

心已入冬 提交于 2019-12-04 03:51:02
Let's say I have the following, public class Foo{ private String bar; public String getBar(){ return bar; } public void setBar(String bar){ this.bar = bar; } } Are these methods automatically threadsafe due to the immutable nature of the String class, or is some locking mechanism required? No, this is not threadsafe. Foo is mutable, so if you want to ensure that different threads see the same value of bar – that is, consistency – either: Make bar volatile , or Make the methods synchronized , or Use an AtomicReference<String> . The reads and writes of bar are themselves atomic, but atomicity is

Why is Java's BigDecimal class not declared as final?

淺唱寂寞╮ 提交于 2019-12-04 03:49:37
While checking the source code of Java's BigDecimal class, I was surprised that it was not declared as a final class : Class BigDecimal public class BigDecimal extends Number implements Comparable<BigDecimal> Immutable , arbitrary-precision signed decimal numbers. (from the Oracle Docs ) Is there a specific reason for this or did the developers just forget to add that keyword? Is it a good practice to not declare immutable classes as final? The same goes for BigInteger , but not for String which is declared as final. Quote from https://blogs.oracle.com/darcy/entry/compatibly_evolving

Arrays implementation in erlang

天大地大妈咪最大 提交于 2019-12-04 03:47:42
问题 My question is, how are arrays implemented in Erlang, as opposed to lists. With immutable types doing things like, move ([X | Xs], Ys) -> [X | Ys]. Ls = move([1,2,3], [2,3,4]) would take up constant mem in the heap, since this is all reference work. But for the same stuff in arrays move (A1, A2) -> array:set(0, array:get(0,A1),A2). A1 = array:from_list([1,2,3]). A2 = array:from_list([0,2,3,4]). A3 = move(A1,A2). Will move here use size proportional to A2 or will it be able to do this in