4
$\begingroup$

Question

Is there a way to define a function which evaluates itself regardless of HoldAll, HoldFirst, ... ?

Background

I'm writing a function which makes it easier to document other functions using ::usage, SyntaxInformation and code completion. I also use code generation and I want to document the generated code.

Therefore I need to define new symbols with a usage message and remove them like this:

(* I already get an error message here since `Evaluate` wants to evaluate
  `Symbol[name]` but `name` is not a string yet *)
NewSymbol[name_String] := Evaluate[Symbol[name]];

Remove[NewSymbol["test"]] (* DOES NOT WORK *)
NewSymbol["test"]::usage = "This is a test"; (* DOES NOT WORK *)


(* In order to make it work I have to use an additional `Evaluate`*)

(* The same is true if I want to use `Remove` *)
Remove[Evaluate[NewSymbol["test"]]]

Evaluate[NewSymbol["test"]]::usage = "This is a test";
$\endgroup$
2
  • 2
    $\begingroup$ What is the purpose of this? What goal is it intended to serve? With that info, answers might be more helpful. $\endgroup$ Apr 8 '20 at 13:08
  • $\begingroup$ @MariusLadegårdMeyer Thank you for your comment. Is the intention clear now? $\endgroup$
    – Qbyte
    Apr 8 '20 at 13:16
8
$\begingroup$

Stay away from Evaluate altogether and use With to inject into functions that hold their arguments:

createSymbolAndSetUsage[sym_String, usage_String] := With[
    {symbol = Symbol @ sym},
    symbol::usage = usage;
    symbol
]

createSymbolAndSetUsage["Foo", "Foo does what it does"]
(* Foo *)

enter image description here

$\endgroup$

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.