What is a “mostly complete” (im)mutability approach for C#? [closed]
Would the better solution be to just use F# where you want true immutability?
Would the better solution be to just use F# where you want true immutability?
The extend() method appends to the existing array and returns None. In your case, you are creating an array — [4, 5, 6] — on the fly, extending it and then discarding it. The variable b ends up with the return value of None.
Are you looking for Sets.immutableEnumSet (Guava) perhaps? Returns an immutable set instance containing the given enum elements. Internally, the returned set will be backed by an EnumSet. The iteration order of the returned set follows the enum’s iteration order, not the order in which the elements appear in the given collection.
It is possible to create immutable dict using just standard library. from types import MappingProxyType power_levels = MappingProxyType( { “Kevin”: 9001, “Benny”: 8000, } ) See source of idea with more detailed explanation
User classes are considered mutable. Python doesn’t have (absolutely) private attributes, so you can always change a class by reaching into the internals. For using your class as a key in a dict or storing them in a set, you can define a .__hash__() method and a .__eq__() method, making a promise that your class … Read more
First of all, new List() won’t work, since the List class is abstract. The other two options are defined as follows in the List object: override def empty[A]: List[A] = Nil override def apply[A](xs: A*): List[A] = xs.toList I.e., they’re essentially equivalent, so it’s mostly a matter of style. I prefer to use empty because … Read more
Guava contributor here. Guava doesn’t currently have an ImmutableEnumMap variant, but if it did, it would probably just be a wrapper around an EnumMap. (That said, slightly better immutable implementations are possible.) EnumMap will perform better than the basic ImmutableMap, in any event; it’s difficult or impossible to beat. (I’ll file an issue to investigate … Read more
Short answer: you’re doing it right. ReadonlyMap<K, V> is essentially a supertype of Map<K, V> since the methods and properties it does have match up with those of Map<K,V>. So by returning a Map as a ReadonlyMap, all you’re doing is widening the type of the value. By the way, that means you can skip … Read more
Yes, GHC’s RTS uses thunks to implement non-strict evaluation, and they use mutation under the hood, so they require some synchronisation. However, this is simplified due to the fact that most heap objects are immutable and functions are referentially transparent. In a multithreaded program, evaluation of a thunk proceeds as follows: The thunk is atomically† … Read more
Bloch’s “Failure atomicity” means that if a method threw an exception, the object should still be usable afterwards. Generally, the object should be in the same state as it was before invoking the method. In the case of an immutable object, you gain that simply from the fact that it’s immutable. There is no operation … Read more