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.

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

NOTE: Package variables should be accessed directly, via their fully qualified names.

Example
/**/  define BeautifulRing(N)
/**/    TopLevel QQ;
/**/    R ::= QQ[b[1..N]];
/**/    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