Why D doesn't have an equivalent to C#'s readonly?

sigod via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 29 15:11:14 PDT 2015


On Monday, 29 June 2015 at 20:12:12 UTC, Assembly wrote:
> I believe it's a design choice, if so, could someone explain 
> why? is immutable better than C#'s readonly so that the 
> readonly keyword isn't even needed? for example, I'd like to 
> declare a member as readonly but I can't do it directly because 
> immutable create a new type (since it's a type specific, 
> correct?) isn't really the same thing.
>
> MyClass x = new MyClass();
>
> if I do
>
> auto x = new immutable(MyClass)();
>
> give errors

Why? I think `const` and `immutable` even better than C#'s 
`readonly`. Also, are you aware that it's recommended to use 
`const` instead of `readonly`?

`new immutable(MyClass)()` is invalid code. Try `immutable 
MyClass x = new MyClass();`.


More information about the Digitalmars-d-learn mailing list