Karim Belabas on Sun, 20 Nov 2011 18:45:30 +0100


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

Re: Backward compatibility


* Bill Allombert [2011-11-20 17:46]:
> On Sun, Nov 20, 2011 at 08:32:53AM -0500, Charles Greathouse wrote:
> > I was trying to write a quick script that would work for both newer
> > (2.4.3 through 2.6.x) and older versions of gp.  For example, I would
> > like to use warning() in the new versions and a print statement in old
> > ones.  I thought it would be convenient to define
> > 
> > if(lex(version(),[2,4,3])<0,{warning(s:str)=print("Warning: "s)});
> > 
> > but this breaks in recent versions -- "variable name expected".  Is
> > there a better way to do this?
> 
> You can do
> if(lex(version(),[2,4,3])<0,eval("warning(s)=print(\"Warning: \"s)"));
> or
> if(lex(version(),[2,4,3])<0,(mywarning(s:str)=print("Warning: "s));alias("warning","mywarning"));
> 
> or you can rename warning to mywarning in your code and do
> if(lex(version(),[2,4,3])<0,mywarning(s:str)=print("Warning: "s),alias("mywarning","warning"));
> 
> (Note that, if you use alias you must do it before reading any code that use
> warning/mywarning).

The problem is that, although the compatibility code is not executed at
runtime [ in post-2.4.3 versions ], it is still compiled, and checked for
problems at compile time. 

And, of course, the construct "f(s) = blah" is forbidden when 'f' is a
built-in function.

In my own code, I use the alias() solution. A simpler possibility is

  if (lex(version(), [2,4,3]) < 0, read("compat.gp"))

where the compatibility code is neither loaded nor compiled unless it's
needed.

Cheers,

    K.B.
-- 
Karim Belabas, IMB (UMR 5251)  Tel: (+33) (0)5 40 00 26 17
Universite Bordeaux 1          Fax: (+33) (0)5 40 00 69 50
351, cours de la Liberation    http://www.math.u-bordeaux1.fr/~belabas/
F-33405 Talence (France)       http://pari.math.u-bordeaux1.fr/  [PARI/GP]
`