Thread overview
[Issue 12512] .dup of const structs does not work
[Issue 12512] Feature request: cdup
Apr 13, 2014
Nick Treleaven
Apr 13, 2014
Walter Bright
Apr 13, 2014
Walter Bright
Apr 14, 2014
Walter Bright
Dec 17, 2022
Iain Buclaw
April 13, 2014
https://issues.dlang.org/show_bug.cgi?id=12512

--- Comment #3 from monarchdodra@gmail.com ---
(In reply to monarchdodra from comment #2)
> (In reply to comment #1)
> > Shouldn't dup just give you const(T)[] when it can't manage T[] ?
> 
> That would also work for me I guess.

Just to be clear, my need is:
Given a type T (which may be qualified), I need a function that can do this:

//----
T[] output = intput.someDup();
//----

Currently, this is not possible in generic code, even with static ifs to switch between dup/idup.

The current workaround right now, is to use `array` instead. It works, but isn't as efficient for arrays (it's specialized for RA only). That said, I could just improve the implementation for arrays...

--
April 13, 2014
https://issues.dlang.org/show_bug.cgi?id=12512

Nick Treleaven <ntrel-public@yahoo.co.uk> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ntrel-public@yahoo.co.uk

--- Comment #4 from Nick Treleaven <ntrel-public@yahoo.co.uk> ---
(In reply to monarchdodra from comment #3)
> Just to be clear, my need is:
> Given a type T (which may be qualified), I need a function that can do this:
> 
> //----
> T[] output = intput.someDup();
> //----

You can just use dup:

    int[] ma = [1, 2, 3];
    immutable(int)[] ia = ma.dup;

I think current dmd can do this because it knows ma.dup is unique, so it can safely convert to immutable.

--
April 13, 2014
https://issues.dlang.org/show_bug.cgi?id=12512

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com
            Version|unspecified                 |D2
           Severity|enhancement                 |normal

--- Comment #5 from Walter Bright <bugzilla@digitalmars.com> ---
The s.dup should work. Marking this as a bug, not an enhancement.

Note that:

    const(int*)[] s;
    s.dup;

does work.

Not going to do a .cdup

--
April 13, 2014
https://issues.dlang.org/show_bug.cgi?id=12512

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Feature request: cdup       |.dup of const structs does
                   |                            |not work

--
April 13, 2014
https://issues.dlang.org/show_bug.cgi?id=12512

monarchdodra@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement

--- Comment #6 from monarchdodra@gmail.com ---
(In reply to Walter Bright from comment #5)
> The s.dup should work. Marking this as a bug, not an enhancement.

It's NOT a bug!

> Note that:
> 
>     const(int*)[] s;
>     s.dup;
> 
> does work.

That's *still* a type where "const(T) : T": "Unqual!(const(int*))" is
"const(int)*".

dup simply *can't* work for certain types. For example, something as trivial as classes:

//----
class A{}

void main()
{
    const(A)[] a;
    a.dup;
}
//----
Error: cannot implicitly convert element type const(A) to mutable in a.dup
//----

> Not going to do a .cdup

Fine. Could we then allow "dup" to return "const(T)[]" for the cases where it can't return a mutable then?

--
April 14, 2014
https://issues.dlang.org/show_bug.cgi?id=12512

--- Comment #7 from Walter Bright <bugzilla@digitalmars.com> ---
(In reply to monarchdodra from comment #6)
> That's *still* a type where "const(T) : T": "Unqual!(const(int*))" is
> "const(int)*".

Ah, you're right.

> > Not going to do a .cdup
> 
> Fine.

Because where does it stop? .sdup for shared dup? .csdup for const shared dup? At some point, it starts looking ridiculous.

> Could we then allow "dup" to return "const(T)[]" for the cases where
> it can't return a mutable then?

I suspect that would be considered surprising behavior.

--
April 14, 2014
https://issues.dlang.org/show_bug.cgi?id=12512

--- Comment #8 from monarchdodra@gmail.com ---
(In reply to Walter Bright from comment #7)
> (In reply to monarchdodra from comment #6)
> > Could we then allow "dup" to return "const(T)[]" for the cases where
> > it can't return a mutable then?
> 
> I suspect that would be considered surprising behavior.

I guess it depends on how you consider "dup" to operate. Arguably, it could just be

"duplicates the array. Strips the qualifiers it can, for convenience".

Under such circumstance, it keeps its existing semantics, but is expanded to support more types than before.

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=12512

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P2                          |P4

--