Skip to content

Commit

Permalink
Merge pull request Macaulay2#3355 from d-torrance/augmented-assignment
Browse files Browse the repository at this point in the history
Remove syntactic sugar for installing augmented assignment methods
  • Loading branch information
DanGrayson authored Jul 12, 2024
2 parents d2f2505 + f3f3d8b commit a7a2f1f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 18 deletions.
5 changes: 0 additions & 5 deletions M2/Macaulay2/m2/methods.m2
Original file line number Diff line number Diff line change
Expand Up @@ -731,11 +731,6 @@ registerFinalizer' = registerFinalizer
registerFinalizer = method()
registerFinalizer(Thing, String) := registerFinalizer'

-- augmented assignment -- syntactic sugar for installing methods
-- e.g., "T += f" is equivalent to "installMethod(symbol +=, T, f)"
scan(augmentedAssignmentOperators, op ->
installMethod(op, Type, Function => (T, f) -> installMethod(op, T, f)))

-- Local Variables:
-- compile-command: "make -C $M2BUILDDIR/Macaulay2/m2 "
-- End:
18 changes: 6 additions & 12 deletions M2/Macaulay2/packages/Macaulay2Doc/doc_augmented_assignment.m2
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@ scan(core "augmentedAssignmentOperators", op -> (
"augmented assignment",
"installing augmented assignment methods"}}))

doc replace("@KEYS@",
concatenate apply(core "augmentedAssignmentOperators", op ->
(newline, 4, "(symbol ", regexQuote toString op, ", Type)")), ///
doc ///
Key
"installing augmented assignment methods"@KEYS@
"installing augmented assignment methods"
Description
Text
In most cases, the default behavior of @TO "augmented assignment"@ gives
Expand All @@ -60,12 +58,8 @@ doc replace("@KEYS@",
Note that an intermediate @SAMP "Foo"@ object was created and then
assigned to @SAMP "x"@. Instead, it would be more efficient if @SAMP "x"@
was modified directly.

The first two lines below do exactly the same thing; the second
line is syntactic sugar for the first.
Example
installMethod(symbol +=, Foo, (x, y) -> (x#0 += y#0; x));
Foo += (x, y) -> (x#0 += y#0; x);
x += y
Text
In some cases, it may be useful to fall back on the default behavior
Expand All @@ -75,17 +69,17 @@ doc replace("@KEYS@",
Bar = new SelfInitializingType of List;
net Bar := x -> net x#0#0;
Bar * Bar := (x, y) -> Bar {{x#0#0 * y#0#0}};
Bar *= (x, y) -> if isMutable x#0 then (
print "using custom method";
x#0#0 *= y#0#0; x) else Default;
installMethod(symbol *=, Bar, (x, y) -> if isMutable x#0 then (
print "using custom method";
x#0#0 *= y#0#0; x) else Default)
x = Bar {new MutableList from {3}}
y = Bar {{4}}
x *= y
y *= x
SeeAlso
"augmented assignment"
"installing methods"
///)
///

doc ///
Key
Expand Down
2 changes: 1 addition & 1 deletion M2/Macaulay2/tests/normal/augmented-assignment.m2
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ assert Equation(x#0, 5)

-- install custom method
Bar - Bar := (x, y) -> Bar(x#0 - y#0)
Bar -= (x, y) -> if even y#0 then x#0 = 0 else Default
installMethod(symbol -=, Bar, (x, y) -> if even y#0 then x#0 = 0 else Default)
x = Bar 5
x -= Bar 2
assert Equation(x#0, 0)
Expand Down

0 comments on commit a7a2f1f

Please sign in to comment.