Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #15366

Re: Unprotect[] Protect[] redefining builtin but keep original available

From Chris Osborn <chrisosb@gmail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Unprotect[] Protect[] redefining builtin but keep original available
Date 2013-09-03 06:20 +0000
Message-ID <l03v38$5dt$1@smc.vnet.net> (permalink)
References <kucorf$a47$1@smc.vnet.net>
Organization Time-Warner Telecom

Show all headers | View raw


Hi Robert,

Yes, this is possible. One solution is:

1. Create a conditional rule for the built-in symbol you would like to override, where the condition simply checks the value of some "override" symbol of your choosing. This allows you to effectively enable/disable the rule by setting the "override" symbol to True/False.

2. Globally, set the value of the "override" symbol to True, so that your rule will be in effect.

3. But, in the *body* of your rule, use a Block to locally set the value of the "override" symbol to False. Everything that executes within the body of the Block will ignore the rule, and the built-in symbol will have its default behavior.


Here is an example:


override = True;

Unprotect[Solve];
Solve[args___] /; override :=
  Block[{override = False},
   Row[{"Here is my solution: ", Solve[args]}]
   ];
Protect[Solve];

Solve[2 x + 4 == 13, x]




Chris


On Tuesday, August 13, 2013 12:56:31 AM UTC-7, roby wrote:
> Dear Group
>
>
>
> Is it in general possible to redefine mathematica builtins but still call the original function from within (but not only from there) the redefinition ?
>
>
>
> In particular I would like to overload a builtin (distinguished by an additional Option additionalopt->o) and keep the possibility to call the original (distinguished by omitting the additional Option or
>
> by setting additionalopt->None)
>
>
>
> Regards Robert

Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Unprotect[] Protect[] redefining builtin but keep original available roby <roby.nowak@gmail.com> - 2013-08-13 07:56 +0000
  Re: Unprotect[] Protect[] redefining builtin but keep original available Chris Osborn <chrisosb@gmail.com> - 2013-09-03 06:20 +0000
  Re: Unprotect[] Protect[] redefining builtin but keep original available Szabolcs Horvát <szhorvat@gmail.com> - 2013-09-04 03:32 +0000

csiph-web