 Type Assertions - Maple Help

::

the :: operator Calling Sequence x :: T Parameters

 x - any expression T - a type specification Description

 • The :: operator serves several purposes in Maple, all related to types.
 • When used in a procedure parameter declaration, the left-hand side of the operator specifies the parameter name, and the right-hand side specifies the expected type of the argument.
 • The return type of a procedure can be declared by following the procedure's parameter declaration sequence by ::T . In this context, :: serves as an assertion that the procedure returns a value of the specified type, T. If assertions are enabled, returning a value not matching the type will raise an exception.
 • When used as the left-hand side of an assignment statement or as the first argument to the assign function, x::T asserts that the value being assigned to x is of type T.
 • A local variable can be declared with a type by using the :: operator. Within the procedure in which the variable is declared, any assignments made to it are treated as if the left-hand side of the assignment were written using the :: declaration.
 • If a local variable was declared with a type, and the left-hand side of an assignment to that local variable also specifies a type, then the value being assigned must satisfy both types.
 • The :: operator can also be used to assert a type on the control variable(s) of a for loop. For example, when iterating over the contents of a container such as a list, the :: operator can assert that each entry is of a specified type.
 • In the condition of an if or while statement, :: serves as a type testing operator. In this context, x::T is equivalent to type(x,T).
 • The :: operator also acts as a type test when used as an argument to any of the Boolean operators, and, or, not, xor, implies, or the evalb function.
 • In any other context, :: evaluates its arguments, but does not itself evaluate any further. Thus, it can be used as a data structure or part of a larger data structure. Although it is not required, it is customary to use :: as a data structure only when the right-hand side represents a type or a type-like concept. For example, see RealRange.
 • An expression using the :: operator is of type ::. • The :: operator is thread-safe as of Maple 15. Examples

A common use of :: is to declare the type of procedure parameters.

 > f := proc( x::integer ) x^2 end proc:
 > $f\left(2\right)$
 ${4}$ (1)
 > $f\left(3.4\right)$

The :: operator can assert the return type of a procedure.

 > f := proc( x::integer ) :: odd; x^2 end proc:
 > $\mathrm{kernelopts}\left(\mathrm{assertlevel}=2\right):$
 > $f\left(3\right)$
 ${9}$ (2)
 > $f\left(4\right)$

Using :: on the left-hand side of an assignment, declaring a local variable with ::, or adding :: to a loop variable, asserts that the value being assigned is of the specified type.

 > $x::'\mathrm{integer}'≔3.4$
 > f := proc( x ) local y::integer;     y := x end proc:
 > $f\left(4.5\right)$
 > $\mathbf{for}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}z::'\mathrm{integer}'\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{in}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\left[1.2,3,4\right]\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}z\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}$

In the condition of an if (or while) statement, the :: operator is equivalent to using the type function.

 > f := proc( x )     if x::integer then         print("an integer")     else         print("not an integer")     end if end proc:
 > $f\left(2\right)$
 ${"an integer"}$ (3)
 > $f\left(3.4\right)$
 ${"not an integer"}$ (4)

The :: operator is inert if used out of context.

 > $x≔2$
 ${x}{≔}{2}$ (5)
 > $x::'\mathrm{integer}'$
 ${2}{::}{\mathrm{Typesetting}}{:-}{\mathrm{_Hold}}{}\left(\left[{'}{\mathrm{integer}}{'}\right]\right)$ (6)

The :: operator is not inert if used within a Boolean expression.

 > $x::'\mathrm{integer}'\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{and}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}x<3$
 ${\mathrm{true}}$ (7)

Expressions involving the :: operator are of type ::.

 > $\mathrm{type}\left(x::\mathrm{symbol},\mathrm{::}\right)$
 ${\mathrm{true}}$ (8)
 > $\mathrm{evalb}\left(\left(x::\mathrm{string}\right)::\mathrm{::}\right)$
 ${\mathrm{true}}$ (9) Compatibility

 • The ability to apply the :: operator to the control variable(s) of a for loop is new in Maple 2021.
 • The :: operator was updated in Maple 2021.