11
$\begingroup$

My template for building packages has, at the end, the following expression that automatically ReadProtects all symbols defined in my pack:

ToExpression[Names["pack`*"], InputForm, SetAttributes[#, {ReadProtected(*,Locked*)}] &]

It works fine, with the exception of cases where I define a constant, like for example: iso=3; This produces the following message (when loading the package):

SetAttributes::sym: Argument 3 at position 1 is expected to be a symbol. >>

Can the automated expression that sets ReadProtected be changed to correct this behavior?

$\endgroup$
11
$\begingroup$

My guess would be this:

ToExpression[
   Names["pack`*"], 
   InputForm, 
   Function[sym,SetAttributes[sym, {ReadProtected(*,Locked*)}],HoldFirst] 
]

The problem is that functions defined with # - & notation do not hold their arguments.

$\endgroup$
  • $\begingroup$ Works fine, thank you! $\endgroup$ – P. Fonseca Apr 30 '12 at 13:25
1
$\begingroup$

This is undocumented so I don't know how stable it is. But worth to point this out.

  • SetAttributes can work on strings if those strings are representing symbols that were already created.

Which is usually the case after running a package. moreover, those symbols don't have to have any type of value, just need to be created when read.

  • The point is, you can just SetAttributes[#,Locked]& /@ Names["Context`*"]

So:

Quit[];

x;
SetAttributes["x", Locked]
Attributes@x
{Locked}

But

Quit[];

SetAttributes["x", Locked]
Attributes@x
SetAttributes["x", Locked]
{}
$\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.