head const (again), but for free?
sighoya
sighoya at gmail.com
Wed Jan 13 19:52:16 UTC 2021
On Wednesday, 13 January 2021 at 19:22:07 UTC, Ola Fosheim
Grøstad wrote:
> On Wednesday, 13 January 2021 at 18:37:02 UTC, sighoya wrote:
>> Yep, head const is usually readonly or better final, readonly
>> should be transitive.
>
> Why? "immutable" is transitive in D. readonly memory or
> registers are not transitive.
The difference is readonly memory didn't know about the type of
data saved in them. A pointer or raw data is in the end the same
thing, just bytes.
Immutable var in high level languages denote a value even if this
value consists of pointers.
Most languages simply don't follow the model.
> No, readonly in technical specifications usually means that it
> is isn't possible to write to it at all. e.g. readonly hardware
> registers. They can still point to writable memory.
Usually, readonly is only a view of a variable, not a part of
type, so you can't change the value of a readonly variable though
the value can be changed from outside.
So we still have a variable whereas for immutable we have what we
call a value.
> "readonly" in Typescript and C# is immutable, not const.
But what happens if I initialize a readonly variable with an
object which I mutate from the outside afterwards?
This shouldn't be possible with immutable because of being
globally unchangeable.
In D you can only pass immutable parts into the initialization of
an immutable aggregate to prohibit any side effect.
But is this the same behavior with readonly in Typescript, I
don't think so.
The proposed readonly in C# would be likewise non immutable as
shown from
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/readonly:
>Because reference types contain a reference to their data, a
>field that is a readonly reference type must always refer to the
>same object. That object isn't immutable.
> Which languages are you thinking of?
C#, Typescript, Rust, Swift.
I think what D offers with immutable is unique, I can't even
count other languages with my fingers providing the same feature
except pure languages.
More information about the Digitalmars-d
mailing list