12
$\begingroup$

I have Echo's buried in code all over my notebook, I'd like a flag to turn them all on or off globally.

  • Sure Unprotect[Echo];Echo=Identity would disable them, but then you can't re-enable them
  • A solution that works for all the various types of Echos (EchoName, EchoEvaluation, ...) would be nice
  • QuietEcho doesn't work because I'd have to write it add it around every blob of code
$\endgroup$
11
  • $\begingroup$ with Echo=., no? $\endgroup$
    – Roma Lee
    Commented Jan 13, 2021 at 18:32
  • $\begingroup$ sure, but how do reenable it without restarting the kernel $\endgroup$
    – M.R.
    Commented Jan 13, 2021 at 18:34
  • 1
    $\begingroup$ Also, you might use Echo = #& instead, since Echo can take multiple arguments. $\endgroup$
    – Carl Woll
    Commented Jan 13, 2021 at 18:42
  • 1
    $\begingroup$ I would advocate for a global search-and-replace of all Echo[ with myEcho in your code, then at the top assign an appropriate value to myEcho, i.e. myEcho = Echo vs. myEcho = Identity depending on what you want. The global search should be relatively safe and painless, since Echo is not a common keyword in other function names. This would also work with all other types of Echo, turning them into myEcho... versions if you wanted. $\endgroup$
    – MarcoB
    Commented Jan 13, 2021 at 18:45
  • 1
    $\begingroup$ @RomaLee This is likely due to Echo being one of the autoloaded symbols. Check, e.g. on a fresh kernel, this: OwnValues[Echo], you will see something like {HoldPattern[Echo] :> System`Dump`AutoLoad[Hold[Echo], Hold[Echo, EchoFunction], "Language`Echo`"] /; System`Dump`TestLoad}. So, initially, Echo has OwnValues. When you execute Echo[0] (actually, just Echo is enough), you cause that definition to run. As a result, OwnValues[Echo] get cleared, instead DownValues[Echo] get populated. When you do Echo = Identity without auto-load, you hopelessly overwrite autoload code. $\endgroup$ Commented Jan 13, 2021 at 19:04

2 Answers 2

12
$\begingroup$

Echo has an autoload, so you need to make sure the symbol is autoloaded before you modify its values:

DisableEcho[] := (Unprotect[Echo]; Echo; Echo = #&; Protect[Echo];)

EnableEcho[] := (Unprotect[Echo]; Echo=.; Protect[Echo];)

Test:

DisableEcho[]

Echo[3]

EnableEcho[]

Echo[3, "EchoLabel"]

3

EchoLabel 3

3

$\endgroup$
1
  • $\begingroup$ Ah, I forgot the autoload $\endgroup$
    – M.R.
    Commented Jan 13, 2021 at 19:22
3
$\begingroup$

I would recommend using QuietEcho rather than redefining Echo:

In[62]:= $Pre = QuietEcho;

In[63]:= Echo[3]

Out[63]= 3

This has the added benefit of disabling printing for all Echo functions, not just Echo.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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