Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Why are the messages almost always defined like this (at least in packages):

If[Not@ValueQ[function::usage], function::usage = "Usage message"]

and not just like this?

function::usage = "Usage message";

Is this some sort of safety measure in case of double loading of the package?
I also notice that error messages, coming after usage messages, are usually defined the second way.
I cannot imagine a reason why I would do that.
Are there situations when someone would like to import a package, redefine some usage messages and be sure they are not reset in case of reloading of the package?

share|improve this question
    
You do not need to do this when writing your own packages. It is a hack to have formatted (and I suppose localised) messages for standard packages. –  Szabolcs Mar 22 '13 at 14:57
add comment

1 Answer 1

up vote 19 down vote accepted

If you look carefully, you'll notice that the usage messages of package functions are nicely formatted.

Mathematica graphics

Notice the nice italicised and subscripted $x_1$. If you actually look in the package, you'll find a usage message that doesn't have any formatting at all, and even differs from the version ?DelaunayTriangulation gives us.

If[Not@ValueQ[DelaunayTriangulation::usage],DelaunayTriangulation::usage =
"DelaunayTriangulation[{{x1,y1},{x2,y2},...,{xn,yn}}] yields the (planar) \
Delaunay triangulation of the points. The triangulation is represented as a \
vertex adjacency list, one entry for each unique point in the original \
coordinate list indicating the adjacent vertices in counterclockwise order."];

So where did the formatting come from? It turns out that Mathematica includes an alternate set of usage messages defined elsewhere. These are nicely formatted. The Not@ValueQ[...] is there to prevent the nicely formatted messages from being overwritten by the package.

The file that contains the formatted messages is located at

$InstallationDirectory/SystemFiles/Kernel/TextResources/English/Usage.m

Use

FileNameJoin[{$InstallationDirectory,
  "SystemFiles", "Kernel", "TextResources", "English", "Usage.m"}]

to get an absolute path from Mathematica.


Regrettably, it seems some errors have slipped in and sometimes the formatted messages contain less information than the original ones... just like in the example above.

share|improve this answer
    
I didn't know about that, +1! I only used the ValueQ method to inject new info to existing messages, for example when using built-in symbols for option names in own package. –  István Zachar Mar 22 '13 at 15:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.