logical const without casts!
Steven Schveighoffer
schveiguy at yahoo.com
Thu Sep 29 11:12:43 PDT 2011
On Thu, 29 Sep 2011 13:26:11 -0400, Steven Schveighoffer
<schveiguy at yahoo.com> wrote:
> On Thu, 29 Sep 2011 13:06:55 -0400, Simen Kjaeraas
> <simen.kjaras at gmail.com> wrote:
>
>> On Thu, 29 Sep 2011 16:54:24 +0200, Steven Schveighoffer
>> <schveiguy at yahoo.com> wrote:
>>
>>> I just thought of an interesting way to make a logical const object
>>> without casts. It requires a little extra storage, but works without
>>> changes to the current compiler (and requires no casts).
>> [snip]
>>> What do people think about this?
>>
>> This is what I think about it:
>>
>> class A {
>> int n;
>> void delegate( ) dg;
>> }
>>
>> pure
>> A createAnA( int n ) {
>> A result = new A;
>> result.n = n;
>> result.dg = (){ result.n++; };
>> return result;
>> }
>>
>> void main( ) {
>> immutable A tmp = createAnA( 3 );
>> assert( tmp.n == 3 );
>> tmp.dg();
>> assert( tmp.n == 3 );
>> }
>
> I agree this breaks immutability, and needs to be addressed. I think
> probably implicit casting of delegates (or items that containe
> delegates) to immutable from strong-pure functions should be disallowed.
>
> But it's not the pattern I described, and uses a relatively new trick
> (implicit immutable casting). I'll file a bug for this case.
>
http://d.puremagic.com/issues/show_bug.cgi?id=6741
-Steve
More information about the Digitalmars-d
mailing list