2

Obviously, Plus[a,b] is given by Mathematica but say I have a package and I want to name a function Plus[a,b]. How can I go about making a new function without overwriting the original function Plus[a,b] given from System. I figured it might be something like this:

BeginPackage["Test`"]

Test`Plus::usage = "Plus[a_,b_] gives..."

(* function defs *) Test`Plus[a_,b_] := ...

EndPackage[]

Or maybe it has something to do with Unprotect[]? Ideally, I could still type in Plus[a,b] and make use of the system function but I could also type in

Test`Plus[a,b]

to make use of the new Plus function from the package. Thanks

EDIT: in response to Marius Ladegård Meyer Is this what you are suggesting I do?

BeginPackage["Test`"]

Plus::usage = "asdf"

Begin["Private"] Plus[a_,b_] := adfasf End[]

EndPackage[]

And then I can use Test`Plus?

Brandon
  • 101
  • 6
  • I think Begin/End and BeginPackage/EndPackage are the way to go here. Using Unprotect on such a central function as Plus is an extremely bad idea, since it will modify a huge number of other (behind the scenes) Mathematica functions. – Marius Ladegård Meyer Jun 25 '20 at 19:13
  • Sorry, what do you mean Begin/End? Like Begin["`Private`"] or something? – Brandon Jun 25 '20 at 19:15
  • 2
    Yes. Have you seen this Workflow guide? – Marius Ladegård Meyer Jun 25 '20 at 19:18
  • So then are you saying this is what I need? (see edit in post) – Brandon Jun 25 '20 at 19:28
  • I do not actually plan to modify Plus, that was just an example. In my instance I am reading The Mathematica Programmer by Maeder and in chapter 5 his database code uses a lot of functions that have preexisting definitions. The book was written for like Mathematica v 2 so its possible those functions didn't exist back then but I'm pretty certain they must have. Some of the functions he uses in his package are "Tuples", "Select", "Union", "Complement", and many more, all of which have preexisting definitions and likely did back then too – Brandon Jun 25 '20 at 19:34
  • 3
    The best practice here is to call your function something other than Plus. – QuantumDot Jun 25 '20 at 20:27
  • Functions related to context change in v3. You may want to reat this: https://mathematica.stackexchange.com/q/43381/1871 – xzczd Jun 26 '20 at 03:30

0 Answers0