0
$\begingroup$

I wrote a quick and dirty little function freshStart which does a variety of things meant to give me a nearly fresh Kernel without having to Quit the kernel. I want the function to have the Option of overriding the default OptionValue symbols \[Rule] "Global\'*" with a list of symbols to be removed. Problem is that Remove doesn't seem to play well with Sequence. How can I fix this?

Below is my little function...

freshStart::usage = 
  "freshStart resets $Line numbers, ClearSystemCache, and disposes of \
Symbols.";
Options[freshStart] = {symbols -> "Global`*", quit -> True};
freshStart[n_ : -2, OptionsPattern[]] := Block[{},
   Unprotect[In, Out];
   Clear[In, Out];
   Protect[In, Out];
   ClearSystemCache[];
   $Line = n;
   If[OptionValue@quit, Quit[], Remove@Sequence@OptionValue@symbols] //
     Echo
   ];

Here is the result of using Sequence. Notice that xyz and abc remain symbols even after running freshStart[] to remove then...

xyz = 123
abc = "my abc's"
echo@symbolQ@xyz;
echo@symbolQ@abc;
freshStart[n = 0, symbols -> {xyz, abc}]
echo@symbolQ@xyz;
echo@symbolQ@abc;
xyz
abc
$\endgroup$
1
  • 2
    $\begingroup$ Wouldn't one have to apply Sequence to a list, i.e., use Sequence@@OptionValue@symbols? But even that would not work becaue if Sequence is evaluated, then also the symbols are evaluated. So Remove would be called like this: Remove[123, "my abc's"] and so nothing but an error message happens. $\endgroup$ Mar 14, 2021 at 7:26

1 Answer 1

2
$\begingroup$

Include Print["opt=", OptionValue@symbols]; in your function and you will see, that the options are already evaluated. Therefore, you need a function with one of the Hold Attributs: HoldFirst, HoldRest, HoldAll,... to get the symbole name, not its value.

Here is one way to do it:

ClearAll[freshStart];
SetAttributes[freshStart, HoldFirst]
freshStart[symbols_List, n_ : -2] := Block[{},
   Unprotect[In, Out];
   Clear[In, Out];
   Protect[In, Out];
   ClearSystemCache[];
   $Line = n;
   Remove /@ Unevaluated[symbols];
   ];

abc = "my abc's";
xyz = 123;
freshStart[{abc, xyz}]

Then when you e.g. say:

?? xyz

You get:

Missing["UnknownSymbol", "xyz"]

$\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.