3
$\begingroup$

I'm having difficulties assigning ::usage in a Mathematica package for symbols in a list or dataset, that will only be defined later in a `Private` subcontext.

In `Private` I have something like this:

list = Dataset[
  {<|"name" -> "Alpha", "age" -> 123|>, 
   <|"name" -> "Beta", "age" -> 456|>, 
   <|"name" -> "Gamma", "age" -> 789|>
}];

Do[Evaluate[Symbol["my" <> list[i, "name"]]] = list[i, "age"], {i, 1, Length[list]}];

How can I now generically specify a ::usage for the symbols myAlpha, etc. before they are even generated? I would like to, as usual, do something like this right after BeginPackage[]

Do[Evaluate[Symbol["my" <> list[i, "name"]]]::usage = "", {i, 1, Length[list]}];

but now obviously list is not yet defined. Can I somehow set this delayed or on hold or anything until the dataset was generated? I couldn't get it working so far.

Thanks alot,

T

$\endgroup$
  • $\begingroup$ Will list be created before you start defining any of my* symbols? In which context should they be created? $\endgroup$ – Kuba Sep 2 '16 at 12:49
  • $\begingroup$ If you need to create the symbols in a specific context, prepend that context to their name (I mean the string in Symbol). $\endgroup$ – Szabolcs Sep 2 '16 at 12:56
  • $\begingroup$ @Kuba Yes, list is created before the my* symbols, just as stated in my question. I'd like them all to be in context Private, but use ::usage to be able to auto-complete the my* symbols with their correctly assigned values ("age" in this case). $\endgroup$ – T_202 Sep 2 '16 at 13:12
  • $\begingroup$ I'm assuming autocomplete in a notebook. So they are not private, they are meant to be Package` . Then, as Szabolcs suggested use "MyPackage`my"<>. $\endgroup$ – Kuba Sep 2 '16 at 13:18
  • $\begingroup$ Is that what you need? $\endgroup$ – Kuba Sep 5 '16 at 5:43
1
$\begingroup$

Is this what you want?

I've changed your Evaluate[Symbol["my" <> list[i, "name"]]] =... to something more flexible. Read more in: How do you programatically load data into symbols?

BeginPackage["Test`"];


Begin["`Private`"];


list = {<|"name" -> "Alpha", "age" -> 123|>, <|"name" -> "Beta", 
    "age" -> 456|>, <|"name" -> "Gamma", "age" -> 789|>};

ToExpression[
  "Test`my" <> #name, StandardForm, Function[name, name = #age, HoldFirst]
] & /@ list;


ToExpression[
  "my" <> #name, StandardForm, Function[name, name::usage = "test", HoldFirst]
] & /@ list; 
  (*notice that the second time they are mentioned they will be found on $ContextPath*)




privateSymbol = {myAlpha, myGamma};

End[];

EndPackage[];

enter image description here

I've dropped Dataset till this problem is solved: Problems with Dataset's querying on a fresh Kernel

$\endgroup$
  • $\begingroup$ Thanks alot @Kuba, that helps and also thanks for pointing out the bug with the Datasets! With respect to this, hopefully this also helped others. $\endgroup$ – T_202 Sep 12 '16 at 8:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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