protect a name from modification - Maple Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : System : Utilities : protect

protect - protect a name from modification

unprotect - undo name protection

Calling Sequence

protect(name1, name2, ...)

unprotect(name1, name2, ...)

Parameters

name1, name2, ...

-

names

Description

• 

protect is used to prevent names from being modified either in an interactive session, user or library code. Most of the names initially known to Maple are protected by default.

• 

Attempting to assign to a protected name will raise an error.  There are two ways to circumvent this error, either use the unprotect command, or declare the name as a "local" variable.

  

Using unprotect can be dangerous as a subsequent assignment will override the default value of that name which will effect internal algorithms that may expect and rely on the default value for correctness. By the same token, being able to unprotect and reassign new values to methods in Maple gives you a powerful tool for extending the system.

  

Variables assigned to within procedures are implicitly understood as being local, in which case you would not run into a conflict with a protected name.  At the top-level, you can declare a variable as local by using the statement, local X;.  This will create a new version or binding of the name, X. The original protected name in this case is still accessible using :-X.  In general the original name is always available using the :- prefix, except for the case of the imaginary unit, I. Declaring I to be local will always give you a warning telling you what the new global name is, favoring, _I, if available.  To remove the local X use the command unbind(X).

• 

A warning will be displayed when declaring and assigning to local X at the top-level only if X had been previously used in the session.  To avoid this warning declare local X prior to using it.  

• 

protect has no effect on environment variables.

Examples

Protected Names

An error is raised when trying to assign to a protected name.

π:=3.14

Error, attempting to assign to `Pi` which is protected.  Try declaring `local Pi`; see ?protect for details.

solve:=x2

Error, attempting to assign to `solve` which is protected.  Try declaring `local solve`; see ?protect for details.

Unprotect

Redefining protected names can affect results.  Consider the correct value of arctan(1).

evalarctan1

14π

(1)

Observe what happens when the constant Pi is changed to 1.

unprotectπ

π:=1

π:=1

(2)

evalarctan1

14

(3)

Protecting Your Own Names.

Important variables and constants can be protected.

myname:=123

myname:=123

(4)

protect'myname'

myname:=456

Error, attempting to assign to `123` which is protected.  Try declaring `local 123`; see ?protect for details.

unprotect'myname'

myname:=789

myname:=789

(5)

Multiple names can be protected at once.

first:=1

first:=1

(6)

second:=2

second:=2

(7)

protect'first','second','π':

second:=3

Error, attempting to assign to `2` which is protected.  Try declaring `local 2`; see ?protect for details.

Creating Local Names

Local versions of names can be created that overshadow the protected version.

local length:=0.001

Warning, A new binding for the name `length` has been created. The global instance of this name is still accessible using the :- prefix, :-`length`.  See ?protect for details.

length2

0.000001

(8)

The original procedure is still available as :-length.

:-lengthabc

3

(9)

The imaginary unit is special.  When overridden with a local, the global version can be accessed via _I.

local I:=1|0,0|1

Warning, The imaginary unit, I, has been renamed _I

I

(10)

I2

1001

(11)

_I2

1

(12)

interfaceimaginaryunit

I

(13)

See Also

environment, initialconstants, initialfunctions, local, name, type[protected], unassign, with


Download Help Document

Was this information helpful?



Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam