Use dup on Containers with const Elements

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jul 31 04:38:02 PDT 2016


On 7/30/16 5:21 PM, Q. Schroll wrote:
> On Friday, 29 July 2016 at 19:24:59 UTC, Steven Schveighoffer wrote:
>> On 7/29/16 3:00 PM, Q. Schroll wrote:
>>> Cases to consider: Arrays and AAs with const(T) Elements, where T is a
>>> value or a reference type respectively.
>>>
>> [snip]
>>> Questions:
>>> (1) Why do I have to specify the type here? Why does inference fail?
>>> (2) Why not just S[S]?
>>> The copy of a const S is a S so why is the copy of a (const S, const
>>> S)-pair not just (S, S)?
>>>
>>
>> array.dup has the meaning to copy the original but make the elements
>> mutable. At least, that's what it was when it was handled by the
>> compiler/runtime.
>
> I do understand the reasons why I can't simply copy const reference type
> objects to mutable. It just makes sense as the referred object is still
> const.
> I thought of dup being there for convenience and performance reasons.
> The spec says about dup: "Create a dynamic array of the same size and
> copy the contents of the array into it."
> It has not been clear to me it intends to make the elements mutable. For
> my intention, I thought of dup making a shallow copy--which is a deep
> copy on value types so it can drop the const then.

If that intention is not in the docs, then that is an omission. It 
definitely always intends to make the result mutable, and should be an 
error if you can't do that. Much code out there expects it to be this 
way, and would break if it simply copied the const tag.

I went ahead and submitted a PR: 
https://github.com/dlang/dlang.org/pull/1435

-Steve


More information about the Digitalmars-d-learn mailing list