The function works on a laptop, but not as part of the package. Function argument message protected

The following code works when evaluating in a notebook:

Person[name_String, age_Integer] := {name, age}; Person["Jane", 30] name = "Dick"; age = 28; Person[name, age] 

Output

 {"Jane", 30} {"Dick", 28} 

So, I put it in the following package:

Person.m

 BeginPackage["Person`"] Unprotect @@ Names["Person`*"]; ClearAll @@ Names["Person`*"]; Person[name_String, age_Integer] := {name, age}; Protect @@ Names["Person`*"]; EndPackage[] 

Person.nb

 Needs["Person`"]; Person["Jane", 30] name = "Dick"; age = 28; Person[name, age] name1 = "Bill"; age1 = 40; Person[name1, age1] 

Output

 {"Jane", 30} Set::wrsym: Symbol name is Protected. >> Set::wrsym: Symbol age is Protected. >> Person[name, age] {"Bill", 40} 

I do not understand why there is a protection problem using character names and age. Are personal arguments "name" and "age" also protected?


celtschk's answer let me see the light. The name and age are not listed below:

 BeginPackage["Person`"] Unprotect @@ Names["Person`*"]; ClearAll @@ Names["Person`*"]; Person::usage = "Person"; Begin["`Private`"] Person[name_String, age_Integer] := {name, age}; End[] Protect @@ Names["Person`*"]; EndPackage[] 
+4
source share
1 answer

Since when you define your Person function, the current context of Person` , all new identifiers are created there, even the identifiers name and age (you did not create them before, therefore they are created at this moment). Subsequently, you protect everything in the context of Person` , including these two characters. When you try to assign them, Mathematica correctly complains that they are protected.

+4
source

Source: https://habr.com/ru/post/1390885/


All Articles