2

Is there a way to use With as follows?

variables := {a = 2, b = 3};
With[variables, a^b]

Desired output

8

Obviously the above code will actually just output

With[variables, a^b]

I know I can do something similar to this by defining a list of rules and using the operator form of ReplaceAll, I just like the way the With syntax looks and would prefer to use it if possible.

Diffycue
  • 1,834
  • 8
  • 11

1 Answers1

4

A quick idiomatic approach is to do

Unevaluated[explicitExpression] /. OwnValues[variableWithSetDelayedDefinition]

You can find it in 76917 together with alternatives. See linked topics as well.

So here:

variables := {a = 2, b = 3};

With[variables, a^b] // Unevaluated // ReplaceAll @ OwnValues @ variables

8

Kuba
  • 136,707
  • 13
  • 279
  • 740
  • Linked topic or related ones do not seem to be close enough to be a duplicate but there is probably a duplicate somewhere. – Kuba Nov 08 '19 at 08:17
  • 1
    I reviewed the related questions and found a couple that I think work; let me know if you agree. Incidentally I reopened one of them, despite the fact that I was the one who closed it, because it really doesn't seem like a duplicate to me now. shrug – Mr.Wizard Nov 08 '19 at 09:05