Advice: Declaring the type of an argument

The use of types in Maple is quite different from what you may find in typical computer languages. In Maple, any value, regardless of type, can always be assigned to any variable. You don't have to declare, for example, that x is an array of a certain size - you just create the array and assign it to x . Of course, a function that is designed to operate on an array will not work very well if it operates on something that is not an array. Therefore many functions (whether part of Maple itself or written by users) check the types of their arguments. If the argument is not of the correct type or types, an error message is produced. The simplest way to do this is to declare the allowed types when naming the formal parameters: after the name of a formal parameter, insert :: and the type specification.

Note the difference between this and assume . The assume command is used with symbolic variables that are not assigned values, and simply tells Maple that it can apply rules that would be appropriate when the variable has certain properties (e.g. is an integer). But it does not prevent values that do not have these properties from being assigned to the variable.


> f:= proc(x::integer, y::{list(integer), set(integer)})
local t;
x+add(t,t = y)

In this case x is required to be an integer, and y must be either a list of integers or a set of integers.

> f(u,[3,4]);

Error, f expects its 1st argument, x, to be of type integer, but received u

> f(3,5);

Error, f expects its 2nd argument, y, to be of type {list(integer), set(integer)}, but received 5

> f(3,{4,5});

[Maple Math]

> g:= (f::procedure, r::(string = realcons .. realcons)) -> evalf(Int(f(op(1,r)),r)):

> g(sin,x=0..Pi);

[Maple Math]

See also: assume , Assume doesn't affect solve , Error: ... expects its ... argument, ..., to be of type ..., but received ... , parameter passing , procedures , type , typematch

Maple Advisor Database R. Israel, 1997