immutable T.init, and pointers to mutable data

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Sep 25 10:52:41 PDT 2014


On 9/25/14 10:56 AM, monarch_dodra wrote:
> On Thursday, 25 September 2014 at 13:37:52 UTC, Steven Schveighoffer wrote:

>> But clearly, any code that results in an immutable pointer to mutable
>> data without casts is incorrect. We should start by outlawing such
>> code, and if there are ways we can carve out certain usages, we can do
>> that.
>
> Hum... right, but I meant "T.init" itself would not be valid. As in:
> alias T = immutable(S);
> T t = T.init; //Illegal?

I would say yes, illegal.

There's some interesting issues with this. For instance, what about:

T[] t;
t.length = 2;

This is a runtime function, and the runtime isn't necessarily aware of 
all the intricacies of a type, it has only blunt instruments (namely the 
TypeInfo init data). So it really can't be asked to know what to do.

I'm kind of leaning towards the notion that immutable(S) should just be 
illegal on principle. But that is quite limiting if you wanted to just 
override the type system.

If we had better implicit casting mechanisms, there may have been a way 
to fix this, but I don't know.

> This might be a borderline case, but I kind of figured that that piece
> of code was the *only* one that was universally valid in generic code.

I think it's very borderline, not likely to affect many projects.

-Steve


More information about the Digitalmars-d-learn mailing list