#1794 Implementing toImmutable()

SlimerDude Mon 27 Feb 2012

This is gonna be a simple one word answer but I thought I'd ask anyway...

If I have the following class, many of which I use to collect data, is there any way can I then make it immutable so it may be passed by ref between Actors?

class EntityProperties {
  Type  entityType
  Id    id
  Bool  wotever
  Str[] prop := [,]
}

Or would I have to create another const class that mimics it, with a copy constructor, and pass that? And if so, would there be any performance benefits in doing so, over say, serialisation?

const class EntityProperties {
  const Type  entityType
  const Id    id
  const Bool  wotever
  const Str[] prop
}

Ok, so the 2nd part of the Q may require more than 1 word! ;)

brian Mon 27 Feb 2012

You have to make your class be a const class for it be immutable.

The performance difference between passing immutable messages and serialized messages is staggering. In general immutable messages can handle millions of messages per second. But if using serialization, then the serialization will dwarf the actual messaging overhead.

As a general rule, I now design all my classes as immutable. Sometimes if construction is complicated, I use a mutable help class that builds up all the fields. I find this pattern works quite well. For example look in compilerDoc how I build up an extremely complicated immutable DocPod class using DocPodLoader.

dobesv Wed 29 Feb 2012

Are there any built-in constructs that make it easy to create a modified copy of a const object? (i.e. just tweaking the fields you want to modify)

SlimerDude Wed 29 Feb 2012

I belive this is covered in the topic Immutable setters... with the short answer being no!

Login or Signup to reply.