Bill Allombert on Tue, 19 May 2020 20:24:31 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Globals and GP2C


On Tue, May 19, 2020 at 05:37:30PM +0000, Jacques Gélinas wrote:
> How can I convince GP2C to accept globals like GP does ?
> 
> ======================= file glob.gp
> [A,B]
> rst() = {      \\ reset global vars
>   A = 2020; B = "virus"; return();
> }
> infoglob() = { \\ print global vars
>   print("A = ", A, " B = ", B);
> }
> =======================

The problem is that GEN are stored on the PARI stack which is
reset before printing the GP prompt.

You can do everything on one line:
? init_glob();rst();infoglob()
A = 2020 B = virus

You can also do
global(A:small,B:str);
so that A and B are known not to be GEN
? init_glob()
? rst()
? infoglob()
A = 2020 B = virus

or you can use clone to copy GEN out of the stack:
rst() = {
  A = clone(2020:gen); B = clone("virus":gen);
}

Cheers,
Bill