how to declare an immutable class?

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Aug 12 01:35:54 PDT 2016


On Friday, August 12, 2016 05:25:45 Mike Parker via Digitalmars-d-learn wrote:
> immutable class Foo { ... } is the same as declaring every member
> of Foo as immutable, just as final class Foo { ... } makes every
> method final.

I'm not sure that that's quite the same thing, because there is such a thing
as a final class, because making the class final makes it illegal to derive
another class from it rather than just affecting the class' functions. So,
final does affect the class itself, whereas immutable does not. It's the
same with most of attributes though - @safe, pure, nothrow, etc. They affect
the members but not the class itself. Putting them on the class is the same
as doing

attribute
{
    class C
    {
        ...
    }
}

or the same as

attribute:

class C
{
    ...
}

if there's nothing after the class in the file (since the label syntax
affects everything after it in the file, whereas the braces only affect
what's in the braces).

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list