Message Boards Message Boards

GROUPS:

Unexpected error related to Unprotecting StringPartition

Posted 5 months ago
449 Views
|
4 Replies
|
8 Total Likes
|

Unprotect@StringPartition

StringPartition[n_Integer, d_Integer] := Function[str, StringPartition[str, n, d]]

results in the following error on my machine:

SetDelayed::write: Tag StringPartition in StringPartition[n_Integer,d_Integer] is Protected.

I have added operator definitions to many other built-in functions using this methodology and StringPartition is the first one that gives me this error.

Please let me know if I'm missing something here or if this error message is a bug?

Thanks, Rian

4 Replies

If you look at the definition of StringPartition, you'll see that it is just a stub loading a package, not a real function (in a fresh kernel, of course):

ClearAttributes[StringPartition, {Protected, ReadProtected}]
Definition@StringPartition
StringPartition := System`Dump`AutoLoad[Hold[StringPartition], 
                                 Hold[StringPart, StringPartition], "StringUtilities`"] /; System`Dump`TestLoad

The package redefines StringPartition and protects it again:

StringPartition;
ClearAttributes[StringPartition, {Protected, ReadProtected}]
Definition@StringPartition    
StringPartition[StringUtilitiesDump`args___]:=With[{StringUtilitiesDump`res=StringUtilitiesDump`oStringPartition[StringUtilitiesDump`args]},StringUtilitiesDump`res/;StringUtilitiesDump`res=!=$Failed]

This explains the behavior you observe. The workaround is to preload the package before making any new definitions as I made above. But remember that modifying built-in functions is dangerous and isn't recommened until you know exactly what you do...

Posted 5 months ago

Hi Shenghui,

Thank you for your response.

I am familiar with Curry, however I would like to avoid defining a new symbol (op in your example) and instead would like to add an operator definition to the existing, built-in symbol StringPartition.

Yes you are right, the unexpected behavior I was referring to in my question is exactly the fact that StringPartition for some reason automatically re-protects itself.

Do you know why this is or how to go about fixing it?

Thanks, Rian

From V11.3, I feel comfortable to deal with this type of operator definition with Curry

In[3]:= op=Curry[StringPartition,{3,1,2}][1][2]
Out[3]= Curry[StringPartition,{3,1,2}][1,2]
In[4]:= op["abc"]
Out[4]= {a,c}

I have a quite interesting observation of this function as well:

In[1]:= Attributes[StringPartition]
Out[1]= {Protected, ReadProtected}

In[2]:= Unprotect@StringPartition
Out[2]= {StringPartition}

In[3]:= Attributes[StringPartition]
Out[3]= {ReadProtected}

In[4]:= StringPartition[n_Integer,d_Integer]:=Function[str,StringPartition[str,n,d]]
During evaluation of In[4]:= SetDelayed::write: Tag StringPartition in StringPartition[n_Integer,d_Integer] is Protected.
Out[4]= $Failed

In[5]:= Attributes[StringPartition]
Out[5]= {Protected,ReadProtected}

Somehow the function is automatically protected again. It does not happen for Log in this online doc

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract