Use dup on Containers with const Elements
Q. Schroll via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Jul 30 14:21:26 PDT 2016
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.
> So the reason for 1 is that you can't convert const(C) to just
> C without a cast, so you must specify the type.
The implementation is made so that I have to. This one is obvious
then.
> I'm not certain about AA, as I don't remember how dup was
> defined on them.
>
> -Steve
I just wonder if it is a bug. If it is true, then it's a
misfeature or badly specified function.
More information about the Digitalmars-d-learn
mailing list