How to break const
Mehrdad
wfunction at hotmail.com
Mon Jun 18 07:44:22 PDT 2012
On Monday, 18 June 2012 at 14:41:02 UTC, deadalnix wrote:
> Le 18/06/2012 16:28, Artur Skawina a écrit :
>> It's fine, if you view a delegate as opaque.
>>
>
> No it isn't. You cannot ensure transitivity anywhere. This have
> obvious, and severe drawback for concurrent programing
> (implicit sharing is back) and GC performances (GC can eb crazy
> fast when it come to transitive immutable data, see OCaml's GC
> performances for instance).
>
>> 'this' being const does not preclude accessing the object that
>> 'this'
>> points to via another, mutable, reference.
>>
>> Consider the alternative - you'd have to forbid storing any
>> delegate
>> with a non-const non-value argument inside any object.
>>
>> And "breaking" const would then _still_ be possible and
>> trivial.
>>
>
> No, and your example don't demonstrate that in any way.
> Transitivity is maintained in the example below, because g
> isn't a member of s, and if it were, then the example would
> break at compile time.
>
>> import std.stdio;
>>
>> S*[const(S)*] g;
>>
>> struct S {
>> int i;
>> this(int i) { this.i = i; g[&this] =&this; }
>> void f() const { g[&this].i=666; }
>> }
>>
>> void main() {
>> const s = S(42);
>> writeln(s);
>> s.f();
>> writeln(s);
>> }
>>
>> Yes, D's "pure" could help here, only it's misnamed (even if in
>> this particular case that term would be fitting).
>>
>
> purity is another beast altogether.
Interesting, making the delegate `pure' doesn't change anything
either.
So 'pure' doesn't let you "infer something just by looking at the
code either", right?
More information about the Digitalmars-d
mailing list