When dealing with namespaces in Mathematica (
$ContextPath ...) one is unavoidably confronted with the problem of shadowing (see e.g. question 1, question2). What are good strategies to handle or avoid these problems?
As a package developer, the most obvious strategy is to use unique names, but to be on the save side one would probably need to use very long names and prefixes like
This has drawbacks: the code will be harder to read than necessary, you still can't be sure about uniqueness and the names will be hard to remember and type, just to name some. (And yes, it spoils most of the fun and beauty of Mathematica).
Thus I have used other strategies like using strings instead of symbols for option names. I just have learned that this also has drawbacks and of course it is not applicable to function names.
Another problem is that as a user of a package, you can't control what the symbol names in these are: It might happen that you want to load two packages that define symbols with the same name. It might happen that you want to use a package written for an older version of Mathematica that defines a symbol with a name that meanwhile exists in the
System` context. I have loaded packages (
Needs) and explicitly removed the corresponding context from
$ContextPath in such situations, but I feel that's somewhat hacky.
Are there better ways to deal with this?
(I found an article available by Wagner which discusses some ideas about the topic, but don't think it gives answers to the examples I gave)
since version 13 there is the new feature "$ContextAliases" which allows to use aliases like:
solves exactly the problem I was addressing. This questions and all
answers can thus be considered "deprecated" and are only relevant for version < 13.