Why does list assignment with a packed array result in unpacked values?

Bug introduced in 5.2 or earlier and fixed in 10.1.0

This is another question on the design choices in Mathematica. I understand that without direct reply from the developers it may not be possible to give a definitive and exhaustive answer to "why" questions but such topics have often been fruitful.

Why does list assignment of the form {a, b, c} = tensor where tensor is packed result in unpacked values for a, b, and c?

To illustrate:

packedQ = DeveloperPackedArrayQ;

tensor = RandomReal[99, {3, 5, 7}];

tensor // packedQ


True

{a, b, c} = tensor;

packedQ /@ {a, b, c}


{False, False, False}

It is possible to make the assignments without unpacking the sub-arrays of tensor by manually unpacking the outer list using Apply:

{a, b, c} = List @@ tensor;

packedQ /@ {a, b, c}


{True, True, True}

Why doesn't Set operate like this by default?

That is, why doesn't Set only unpack the right-hand-side as far as necessary, to the level of the left-hand-side?

• Your question raises another in my mind: since tensor already has the head List, why does List @@ tensor have any effect on Set at all, considering it's evaluated before Set even sees it? Jul 22, 2013 at 19:58
• @m_goldberg Because Apply does unpack the top level of the packed array. It does it always, even if the head to be applied is also a List. Jul 22, 2013 at 22:59
• @LeonidShifrin. So, although it's not his main point, one thing Mr.Wizard is telling us is that when Set is given tensor with its top-level unpacked, the lower levels will not be disturbed. Jul 23, 2013 at 2:16
• @m_goldberg Yes, that is what I tried to express that with the final example given. Another way to look at this problem is that apparently when Set gets a packed array on the RHS and a list on the LHS, it fully unpacks the RHS rather that only unpacking it to the level of the LHS. This seems like an unfortunate choice, but I have often learned that there are good reasons for such choices once I asked about them. Jul 23, 2013 at 3:19
• @MichaelE2 I would agree that the special case of List was probably considered not important. In fact, the only reason one would want to do List @@ packed would be if one wants to unpack one level. It also seems to be the only way to do this (i.e. using Apply`), if one wants to only unpack one level. Jul 23, 2013 at 9:10

Thanks Mr.Wizard. I've filed a bug for this, #288440 if you have future correspondence with people inside the company about it...

• This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. Feb 9, 2015 at 19:23
• @m_goldberg Do you think it is reasonable to make an exception for the developers of Mathematica? Given at least some implied authority here this is a form of answer. Feb 9, 2015 at 20:04
• @m_goldberg, since Taliesin has filed a bug one might argue that the question has been answered, albeit indirectly. Why does Mathematica do this? Because it has a bug. (As opposed to it being a deliberate design choice). Feb 9, 2015 at 22:22
• Aside from these meta comments, this has been fixed. Should be in the next release. Feb 10, 2015 at 4:15
• @Mr.Wizard unless we want to decide how many angels can dance on the head of a pin, let's just call it a bug :) Apr 22, 2015 at 5:35