how to declare an immutable class?

Mike Parker via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 11 18:33:35 PDT 2016


On Friday, 12 August 2016 at 00:44:31 UTC, Charles Hixson wrote:
> A way around this, which may be the same as the approach used 
> by string was:
>
> alias immutable(Msg_)    Msg;
> class    Msg_
> {  ...


This is exactly what Jonathan suggested in the post above. And 
yes, it's how string is handled:

alias string = immutable(char)[];

>
> 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.

What problem do you want to avoid? This is a common D idiom. 
Alias names are very short-lived. Yes, it's another symbol in the 
namespace, but it will never make it to the object file. It won't 
even make it to error messages -- you'll see Msg_ there instead.





More information about the Digitalmars-d-learn mailing list