how to declare an immutable class?

Charles Hixson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 11 17:44:31 PDT 2016


A way around this, which may be the same as the approach used by string was:

alias immutable(Msg_)    Msg;
class    Msg_
{  ...

This so far appears to do what I want.  The only problem is that it 
introduces an extraneous symbol, which I would prefer to avoid.

OTOH, I did fix a few problems before this solution
On 08/11/2016 10:56 AM, Charles Hixson via Digitalmars-d-learn wrote:
> I want to declare a class all instances of which will be immutable, 
> and all references to which will be inherently immutable (so that I 
> don't need to slip a huge number of "immutable" statements in my code).
>
> This is surely possible, because string acts just that way, but I 
> can't figure out how to do this.
>
> immutable class    Msg  {    this(...) immutable{...} ... }
>
> doesn't work that way, as when I do
>
> Msg m = new Msg (...);
>
> I get:
>
> Error: incompatible types for ((this.m) - (m)): 'immutable(Msg)' and 
> 'cellram.Msg'
>
> and
>
> Error: immutable method cellram.Msg.this is not callable using a 
> mutable object
>
>
> Does anyone know the correct approach?
>
>



More information about the Digitalmars-d-learn mailing list