Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Conversation

aG0aep6G
Copy link
Contributor

The second overload takes care of all cases where the elements can be
converted to mutable. So, the first overload has no business
Unconst-ing anything.

The second overload takes care of all cases where the elements can be
converted to mutable. So, the first overload has no business
Unconst-ing anything.
@property auto dup(T)(T[] a)
if (!is(const(T) : T))
@property T[] dup(T)(T[] a)
if (!is(const(T) : Unconst!T))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need Unconst here? If T were const it should match the other overload.
It's requiring a global import of core.internal.traits that I'd like to avoid.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other overload only matches when is(const(T) : T). That's not true for const(Object), for example, so it's handled by the first overload.

The difference in the overloads is not that one takes mutable and the other const. Both take all mutable/const/immutable. Convertability to mutable is where they differ.

To check against the convertability, we need to strip any const/immutable from T => Unconst.

@MartinNowak
Copy link
Member

The other overload only matches when is(const(T) : T). That's not true for const(Object), for example, so it's handled by the first overload.

That's intended as you cannot convert constant values with references to mutable, because it would require deep copying.

It doesn't work with const(Object), though ...

Same here, needs deep copying.

@aG0aep6G
Copy link
Contributor Author

It doesn't work with const(Object), though ...

Same here, needs deep copying.

That was about std.array.array. That isn't supposed to convert to mutable, too, is it? How do I just get a shallow copy of an array?

@MartinNowak
Copy link
Member

That was about std.array.array. That isn't supposed to convert to mutable, too, is it? How do I just get a shallow copy of an array?

Ah right, that's a pretty annoying limitation. There is currently no way to declare a tail-const object in D Issue 5325.
You can work with Rebindable!(const Object)[] instead, that will also work with .dup.

@MartinNowak
Copy link
Member

Also see const(FAQ) - D Programming Language.

@aG0aep6G
Copy link
Contributor Author

I don't want tail-const. But we don't have to go through this here now. I made a thread on D.learn: http://forum.dlang.org/thread/lncozysqrhzmrmypkegz@forum.dlang.org#post-lncozysqrhzmrmypkegz:40forum.dlang.org.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants