2
$\begingroup$

I wish to use Echo for output of intermediate results if an OptionValue I call verbose->True and neutralize it if verbose->False. I think this is useful when you need to know if some resources (packages. etc) were successfully loaded. I am thinking that it might work one of two ways...

  1. Spot lines of code with Echo but if verbose->False turn off Echo inside function then turn Echo back on when leaving function.
  2. Spot lines of code with a generic fn variable. When verbose->True activate it with fn=Echo but in its off state it should do something completely inert fn=do-something-that-does-not-effect-expression.

Please help me figure this out. Here is where I would first use it. Notice the If statements that really do nothing more than repeat an Echo wrapped expression. I wish to eliminate the If statements.

$resources=windows-path-to-alternate-directory-for-packages
packs={"package1`","package2`",...}
Options[loadPacks] = {path -> $resources, verbose -> True};
loadPacks[packs_, OptionsPattern[]] := Block[{pathslist},
   addpath[] := Once[PrependTo[$Path, OptionValue[path]]];
   If[OptionValue[verbose] === True, Echo[addpath[]], addpath[]];
   Map[Needs[#] &, packs];
   If[OptionValue[verbose] === True, 
    Echo[Column[contextSymbols[packs]]], Return[]];
   ];
contextSymbols[names__] := 
  Block[{list}, list = If[ListQ[names], names, {names}]; 
   Map[Names[# <> "*"] &, list]];
$\endgroup$
2
  • 2
    $\begingroup$ Consider using your own echo function in the code. Then at the beginning of your function, you can set its value to Echo or Identity depending on the value of the option. $\endgroup$
    – MarcoB
    Commented Nov 11, 2020 at 6:47
  • $\begingroup$ @MarcoB I had already figured it out but I called my "echo" echoResults[expr,label,show->opt] where if show ->True (this is default) will do Echo[expr,label] but if show anything else it returns Evaluate[expr]. Is this potentially problematic? Is Identity a safer option? should I allow for a third option on show ->Echo, Identity, or Evaluate? I can count maybe a dozen times you helped me. Thank you so much. :) $\endgroup$ Commented Nov 11, 2020 at 7:22

0

Your Answer

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