Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2013

[Date Index] [Thread Index] [Author Index]

Search the Archive

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

  • To: mathgroup at smc.vnet.net
  • Subject: [mg131567] Re: Unprotect[] Protect[] redefining builtin but keep original available
  • From: Chris Osborn <chrisosb at gmail.com>
  • Date: Tue, 3 Sep 2013 02:25:24 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-outx@smc.vnet.net
  • Delivered-to: mathgroup-newsendx@smc.vnet.net
  • References: <kucorf$a47$1@smc.vnet.net>

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



  • Prev by Date: Re: problem with mathematica ---> Get::"noopen": "Cannot open ("Graphics`Graphics`'")."
  • Next by Date: Re: MathSource Notebook Smith Normal Form Error - How to Correct?
  • Previous by thread: Re: DynamicModule values not saved correctly in "docked" cells?
  • Next by thread: Re: Unprotect[] Protect[] redefining builtin but keep original available