Re: Default Symbol Names in Package Functions

*To*: mathgroup at smc.vnet.net*Subject*: [mg96151] Re: Default Symbol Names in Package Functions*From*: Albert Retey <awnl at gmx-topmail.de>*Date*: Sat, 7 Feb 2009 03:50:37 -0500 (EST)*References*: <gmec67$a8j$1@smc.vnet.net>

Hi, > So I conclude that one should just not use default arguments that pass > symbol names in package functions. There are too many potential problems. that is basically my experience, too... > Make the user always enter the symbol. Still, I wonder if anyone has some > better idea for doing this? What usually works very well for me in cases like the one you described is to return Functions instead of expressions: foo = Function[{x},1+x+x^2] Of course you can also write functions that return functions. For me that has proofed to be a very clean way to express things like your example. The only drawback is that it demands a little more understanding of Mathematica from the user, but that can be compensated with decent documentation... Depending on what the purpose of your package is, you might want to define something like mypoly[] which behaves like Function when it gets arguments but does e.g. format like 1+x+x^2 ... If you for whatever reason want to stick to expressions instead of functions, you could also consider to create Global`x (or SecondPackage`x if that exists and is on $ContextPath) only when it is actually needed, not when the package is loaded, with something like: foo[var_: None] := With[{x = var /. None :> Symbol["x"]},x^2 + x + 1] Of course that means that your code must be robust enough to work with symbols coming from whatever context x would come from. Other problems I see are that when Global`x or SecondPackage`x have OwnValues, you might not get back an appropriate polynom but only e.g. a number and if you use foo in another package, you might get back yet another x there... hth, albert