In my little scratch built flex game framework, I have defined a class called ThreeDPoint, that carries an x, y, and z co-ordinate set to track my in-game objects, called Actors. I also use the class to create movement vectors that stack up and are added together every frame to create a cumulative movement vector for each Actor.
I made ...
I have a data structure made of Jobs each containing a set of Tasks. Both Job and Task data are defined in files like these:
jobs.txt:
JA
JB
JC
tasks.txt:
JB T2
JA T1
JC T1
JA T3
JA T2
JB T1
The process of creating objects is the following:
- read each job, create it and store it by id
- read task, retrieve job by id, create t...
This is a follow-up now that Scala 2.8.0 beta is out to this question:
http://stackoverflow.com/questions/1710813/what-is-a-proper-way-to-manage-flexible-typed-immutable-data-structures-in-scal
The new technique is to copy a case class, e.g.
case class Person(name:String, email:String)
val bob = Person("Bob", "[email protected]")
val jill = ...
What tools/libraries exist that will take a struct and automatically generate an immutable wrapper and also a "builder" class for incrementally building new instances?
Example input:
struct Foo
{
public int apples;
public int oranges;
public Foo Clone() {return (Foo) base.MemberwiseClone();}
}
Example output:
public clas...
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...
I am writing a small utility for myself so whilst I am using Unity currently, I can change to a different IoC container if it will allow me to get around this problem.
I want to create a class that is given some data when it is created, but is immutable after that, normally I would do this with:
class SomeItem
{
public SomeItem(str...
I have been trying to wrap my head around this FXCop violation "DoNotDeclareReadOnlyMutableReferenceTypes"
MSDN: http://msdn.microsoft.com/en-us/library/ms182302%28VS.80%29.aspx
Code from MSDN which would cause this violation:
namespace SecurityLibrary
{
public class MutableReferenceTypes
{
static protected readonly St...
If I have an immutable Map which I might expect (over a very short period of time - like a few seconds) to be adding/removing hundreds of thousands of items from, is the standard HashMap a bad idea? Let's say I want to pass 1Gb of data through the Map in <10 seconds in such a way that the maximum size of the Map at any once instant is on...
I would like to make an object's structure immutable, preventing its properties from being subsequently replaced. The properties need to be readable, however. Is this possible?
I'm sure there are no language features (along the lines of final in Java and sealed in C#) to support this but wondered whether there might be another mechanism...
I have been reading this question and a few other answers and whilst I get the difference between changing the reference and changing the state of the current instance I'm not certain why this means that I shouldn't mark it readonly. Is this because marking something as readonly tells the compiler something special about the instance an...
I'm going to post a question that I've seen variants of elsewhere, but where I've not quite seen the answer I came up with. I'll subsequently post my answer.
In order to modularize my build script using macros, I would like to put both the updatetask and the task I want to execute if it is not up to date, in the same macro.
How do I do...
Why are copy constructors unnecessary for immutable objects? Please explain this for me.
...
public class GamePiece {
public GamePiece(char cLetter, int nPointValue) {
m_cLetter=cLetter;
m_nPointValue=nPointValue;
m_nTurnPlaced=0; //has not been placed on game board yet.
}
public char GetLetter() {return m_cLetter;}
public int GetPointValue() {return m_nPointValue;}
public int GetT...
To keep things simplified lets say I have an interface RandomProvider interface
public interface RandomProvider
{
double nextRandom();
}
And say I have 3 different implementations of this interface, ARandom, BRandom, CRandom. I want to collect some statistics about the implementations:
how many times nextRandom() is called
sum ...
I showed this struct to a fellow programmer and they felt that it should be a mutable class. They felt it is inconvenient not to have null references and the ability to alter the object as required. I would really like to know if there are any other reasons to make this a mutable class.
[Serializable]
public struct PhoneNumber : IEquata...
I want to have immutable Java objects like this (strongly simplyfied):
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 mutabili...
I understand that NSNumber is immutable, but I still have the need to change it in my app. I use it because it's an object, and therefore usable with @property (if I could, I would use float or CGFloat in a heartbeat, but sadly, I can't).
Is there any way to just overwrite the old NSNumber instance? I'm trying this:
NSNumber *zero = [[...
Let's say we have a memory-intensive class like an Image, with chainable methods like Resize() and ConvertTo().
If this class is immutable, won't it take a huge amount of memory when I start doing things like i.Resize(500, 800).Rotate(90).ConvertTo(Gif), compared to a mutable one which modifies itself? How to handle a situation like th...
Having searched for a way to enforce immutability of custom types and not having found a satisfactory answer I came up with my own shot at a solution in form of a metaclass:
class ImmutableTypeException( Exception ): pass
class Immutable( type ):
'''
Enforce some aspects of the immutability contract for new-style classes:
- a...
Say I have a simple object such as
class Something
{
public int SomeInt { get; set; }
}
I have read that using immutable objects are faster and a better means of using business objects? If this is so, should i strive to make all my objects as such:
class ImmutableSomething
{
public int SomeInt { get { return m_someInt; } }
p...