Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6902259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:46:34+00:00 2026-05-27T07:46:34+00:00

The usage message for Set reminds us that multiple assignments can easily be made

  • 0

The usage message for Set reminds us that multiple assignments can easily be made across two lists, without having to rip anything apart. For example:

Remove[x1, x2, y1, y2, z1, z2];
{x1, x2} = {a, b}

Performs the assignment and returns:

{a, b}

Thread, commonly used to generate lists of rules, can also be called explicitly to achieve the same outcome:

Thread[{y1, y2} = {a, b}]
Thread[{z1, z2} -> {a, b}]

Gives:

{a, b}
{z1 -> a, z2 -> b}

However, employing this approach to generate localized constants generates an error. Consider this trivial example function:

Remove[f];
f[x_] :=
 With[{{x1, x2} = {a, b}},
  x + x1 + x2
  ]
f[z]

Here the error message:

With::lvset: "Local variable specification {{x1,x2}={a,b}} contains 
{x1,x2}={a,b}, which is an assignment to {x1,x2}; only assignments 
to symbols are allowed."

The error message documentation (ref/message/With/lvw), says in the ‘More Information’ section that, “This message is generated when the first element in With is not a list of assignments to symbols.” Given this explanation, I understand the mechanics of why my assignment failed. Nonetheless, I’m puzzled and wondering if this is necessary restriction by WRI, or a minor design oversight that should be reported.

So here’s my question:

Can anyone shed some light on this behavior and/or offer a workaround? I experimented with trying to force Evaluation, without luck, and I’m not sure what else to try.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:46:35+00:00Added an answer on May 27, 2026 at 7:46 am

    What you request is tricky. This is a job for macros, as already exposed by the others. I will explore a different possibility – to use the same symbols but put some wrappers around the code you want to write. The advantage of this technique is that the code is transformed “lexically” and at “compile-time”, rather than at run-time (as in the other answers). This is generally both faster and easier to debug.

    So, here is a function which would transform the With with your proposed syntax:

    Clear[expandWith];
    expandWith[heldCode_Hold] :=
     Module[{with}, 
       heldCode /. With -> with //. {
           HoldPattern[with[{{} = {}, rest___}, body_]] :> 
                  with[{rest}, body],
           HoldPattern[
             with[{
               Set[{var_Symbol, otherVars___Symbol}, {val_, otherVals___}], rest___}, 
               body_]] :>
                  with[{{otherVars} = {otherVals}, var = val, rest}, body]
         } /. with -> With]
    

    Note that this operates on held code. This has the advantage that we don’t have to worry about possible evaluation o the code neither at the start nor when expandWith is finished. Here is how it works:

    In[46]:= expandWith@Hold[With[{{x1,x2,x3}={a,b,c}},x+x1+x2+x3]]
    Out[46]= Hold[With[{x3=c,x2=b,x1=a},x+x1+x2+x3]]
    

    This is, however, not very convenient to use. Here is a convenience function to simplify this:

    ew = Function[code, ReleaseHold@expandWith@Hold@code, HoldAll]
    

    We can use it now as:

    In[47]:= ew@With[{{x1,x2}={a,b}},x+x1+x2]
    Out[47]= a+b+x
    

    So, to make the expansion happen in the code, simply wrap ew around it. Here is your case for the function’s definition:

    Remove[f];
    ew[f[x_] := With[{{x1, x2} = {a, b}}, x + x1 + x2]]
    

    We now check and see that what we get is an expanded definition:

    ?f
    Global`f
    f[x_]:=With[{x2=b,x1=a},x+x1+x2]
    

    The advantage of this approach is that you can wrap ew around an arbitrarily large chunk of your code. What happens is that first, expanded code is generated from it, as if you would write it yourself, and then that code gets executed. For the case of function’s definitions, like f above, we cansay that the code generation happens at “compile-time”, so you avoid any run-time overhead when usin the function later, which may be substantial if the function is called often.

    Another advantage of this approach is its composability: you can come up with many syntax extensions, and for each of them write a function similar to ew. Then, provided that these custom code-transforming functions don’t conlict with each other, you can simply compose (nest) them, to get a cumulative effect. In a sense, in this way you create a custom code generator which generates valid Mathematica code from some Mathematica expressions representing programs in your custom languuage, that you may create within Mathematica using these means.

    EDIT

    In writing expandWith, I used iterative rule application to avoid dealing with evaluation control, which can be a mess. However, for those interested, here is a version which does some explicit work with unevaluated pieces of code.

    Clear[expandWithAlt];
    expandWithAlt[heldCode_Hold] :=
     Module[{myHold},
        SetAttributes[myHold, HoldAll];
        heldCode //. HoldPattern[With[{Set[{vars__}, {vals__}]}, body_]] :>
         With[{eval = 
                  (Thread[Unevaluated[Hold[vars] = Hold[vals]], Hold] /.
                       Hold[decl___] :> myHold[With[{decl}, body]])},
           eval /; True] //. myHold[x_] :> x]
    

    I find it considerably more complicated than the first one though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the basic usage of std::tr1::aligned_storage ? Can it be used as auto
I am studying usage of web services nowadays. Can anyone recommend some free web
I have an application with set limits on subscription attributes i/e a user can
I got a script from the Net that computes the usage of datafiles and
I have hundreds of properties that need to be set in an object, which
In args4j I define options like that: @Option(name=-host,usage=host to connect) @Option(name=-port,usage=port of the host)
ask_username = True ask_password = True ask_message = True ask_number = True def Usage():
Our usage case is a database responsible for accounts, sessions, licenses, etc. — it
My usage-scenario may seem a bit unusual, but here it is: When using vim
What level of CPU usage should be considered high for SQL Server? ie 80%

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.