4
$\begingroup$

The following command assigns 1 to a, 2 to b and 3 to c:

{a,b,c} = Range[3]; {a,b,c}
(* {1, 2, 3} *)

But inside Block, Module or With it doesn't work, because "only assignments to symbols are allowed"

Block[{{a,b,c} = Range[3]}, {a,b,c}]
Block::lvset: "Local variable specification {{a,b,c}=Range[3]} contains {a,b,c}=Range[3], which is an assignment to {a,b,c}; only assignments to symbols are allowed."

Since:

Those scoping constructs take symbols as elements of the first argument, optionally setting them to values. Whether that could be extended to nested lists is an open question I guess, but the fact is the language does not support that at this time. – Daniel Lichtblau

Is there any reason the syntax cannot be extended to this form? What's the difference between evaluating the same command inside and outside the scoping construct?

$\endgroup$
3
  • 2
    $\begingroup$ Those scoping constructs take symbols as elements of the first argument, optionally setting them to values. Whether that could be extended to nested lists is an open question I guess, but the fact is the language does not support that at this time. $\endgroup$ – Daniel Lichtblau Sep 15 '14 at 18:35
  • 2
    $\begingroup$ The first argument of Block (and also Module, With) is a declaration list. Assignments in declaration list are subject to a different semantics than assignments in the body of the scoping construct. Massive assignments of the type you described are simply not allowed in scoping constructs, syntactically. A link to related discussion. $\endgroup$ – Leonid Shifrin Sep 15 '14 at 18:39
  • 2
    $\begingroup$ You can always do Block[{a,b,c},{a,b,c} = Range[3]] $\endgroup$ – rhermans Sep 15 '14 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.