I was wondering if it's possible to write a function that evaluates an
expression in a particular context. In other words, instead of
writing this:
cpsave = $ContextPath;
Begin["Materials1`"];
$ContextPath = {"Materials1`", "System`"};
myExpression;
End[];
$ContextPath = Global`cpsave;
I would like to be able to make a function call, e.g.
evaluateInContext["Materials1`",myExpression]
The naive solution of
evaluateInContext[cont_,expr_]:=(
cpsave = $ContextPath;
Begin[cont];
...
)
doesn't work because any symbol in parentheses is bound to the current
context before any evaluation takes place. Is there a trick to make
this work?
Thanks, and happy New Year to all!
--Leo
SNIP
I wondered about the same a few months ago:
(Leonid Shifrin gave a helpful response.)
Vince Virgilio
Remove[f, x];
make[con_, myExp_] := (
BeginPackage[con];
Print["context=", $Context];
ToExpression@myExp;
EndPackage[];
)
make["con1`", "f[x_]:=x^2"]
Daniel