Strange behavior of the Pattern with a Paclet

I get the data from the file PacletInfo.m and I want to process them using a Pattern. For example, let's try to get the context.

paclet = Paclet[
Name -> "PacletName",
Extensions -> {{"Kernel", Context -> "ContextName"}}
];

getContext[Paclet[
___,
Extensions -> {___, {"Kernel", ___, Context -> name_String, ___}, ___},
___
]] := name

getContext[paclet]

(* "ContextName" *)


This is worked. Now I want to create a separate definition for the Pattern:

pacletPattern[context_Pattern] :=
Paclet[
___,
Extensions -> {{"Kernel", ___, Context -> context, ___}},
___
];


Now you can define the function in this way:

getContext2[pacletPattern[context_String]] := context


It works again. But it's unclear why this does not work without defining functions:

pacletPattern[context_String]

ReplaceAll::reps: {___,Extensions->
{{Kernel,___,Context->context_String,___}},___} is neither a list
of replacement rules nor a valid dispatch table, and so cannot be used for
replacing.

ReplaceAll::reps: {___,Extensions->
{{Kernel,___,Context->context_String,___}},___} is neither a list of
replacement rules nor a valid dispatch table, and so cannot be used for
replacing.

• Are you sure you mean to force the Head Pattern with context_Pattern? – Mr.Wizard Apr 27 '17 at 12:23
• This is just an example. But in general, yes. I want to use functions that return different patterns depending on the arguments – Kirill Belov Apr 27 '17 at 14:52

I believe the problems come from the fact that Paclet has associated definitions, including formatting code, which is triggered by your example.

I am also very skeptical about your way of trying to parse this information. What if Extensions is present as "Extensions" instead? What if someone used the undocumented short form E? Etc.

Just do not do this. Dealing with paclets should be done with utmost care because they are undocumented, and not designed for end users. What you are doing is asking for trouble. I strongly suggest you stick to the API.

Use the PacletInformation function:

rules = PacletInformation["/path/to/PacletInfo.m"]


This gives you a rule list form which you can get the information conveniently.

Lookup[rules, "Context"]


If you want a Paclet expression, use the CreatePaclet function. Comments in the paclet manager source say:

ALL reading of PacletInfo.m files into Mathematica should be done by this function. The trick we use is to read the files in such a way that Paclet and the other symbols (Name, Version, etc.) are created in a private context. Then we turn them into strings, except for Paclet. Thus, this function returns an expression like {Paclet["Name"->"foo", "Version"->"1.0", ...]} where there are no symbols, only strings, except for System symbols like True, False, All.

Thus you should use

paclet = CreatePaclet["/path/to/PacletInfo.m"]


This will verify the contents of the file and create a consistent Paclet expression. Then you can e.g.

paclet["Name"]


to get the name or

paclet["Extensions"]


to get the extensions.

However, the most convenient way would still be PacletInformation[paclet], so why not use that function directly on the PacletInfo.m file?

Note that the Paclet expression you are trying to work with is not a valid one. Name should be "Name" instead, otherwise things won't work. If you Get the PacletInfo.m directly, then you will not only end up with an invalid Paclet expression, but also create several symbols (like Name). This is probably undesirable.

• Yep, it breaks trying to format Paclet[...], a quick fix would be e.g. Verbatim[Paclet][___,.... – Kuba Apr 27 '17 at 18:02
• @Kuba It would still not be the correct way to deal with a PacletInfo.m ... – Szabolcs Apr 27 '17 at 18:03
• That is why I didn't answer after you. – Kuba Apr 27 '17 at 18:11
• @Szabolcs, Many thanks! PacletInformation is what suits me. I realized that this was an wrong method, but the problem was that the functions of PacletManager were not documented – Kirill Belov Apr 27 '17 at 19:10
• @KirillBelov Yes, that makes it pretty difficult, unfortunately. There is some documentation within the source files. It's in SystemFiles/Autoload. – Szabolcs Apr 27 '17 at 19:13