up previous next
TopLevel    --    make a top-level variable accessible inside a function


Syntax
TopLevel X;
  where X is the name of a top-level variable or function.

Description
This command makes a top-level variable accessible from inside a function. For instance, it is useful for making the rings QQ and ZZ accessible, and also if a top-level function is to be passed as a parameter (e.g. to the function SortBy ). It is poor style to use TopLevel for purposes other than these.

A fully qualified package variable can be used directly in a function; the TopLevel command is not needed in this case (and gives an error if a fully qualified package name is specified).

See also the commands ImportByRef, ImportByValue if you want to access a non-top-level variable inside an anonymous function.

Example
/**/  define BeautifulRing(N)
/**/    TopLevel QQ;
/**/    R ::= QQ[b,e,a,u,t,y];
/**/    return R;
/**/  enddefine;


/**/  define CompareLen(X,Y) return len(X) < len(Y); EndDefine;
/**/
/**/  define LongestName(ListOfNameAndValue)
/**/    TopLevel CompareLen;  --> to pass it as paremeter to SortBy
/**/    names := [entry[1] | entry in ListOfNameAndValue];
/**/    SortBy(ref names, CompareLen);
/**/    return last(names);
/**/  EndDefine;
/**/
/**/  L := [["ABC",1],["XYZT",2]];
/**/  LongestName(L);
XYZT

See Also