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$ Commented Apr 8, 2020 at 13:08
  • $\begingroup$ @MariusLadegårdMeyer Thank you for your comment. Is the intention clear now? $\endgroup$ Commented Apr 8, 2020 at 13:16

1 Answer 1

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$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.