

To my knowledge, UpValues can not be used to solve this problem, which is a pity, since having a usual = syntax with custom assignment code for various data types would be convenient. Is too deep for an assigned rule to be found. TagSetDelayed::tagpos: Tag myType in (var_=rhs_)/ MatchQ And because we can not associate the definition with tags deeper than on the first level of the expression, the following won't work either: ClearAll The problem is that Set holds its l.h.s., so by the time the pattern-matching happens, it only has the symbol a, not its value. I just want to stress one important and rather non-obvious consequence of it: it looks like you can not use UpValues to overload assignment ( Set operator) so that it would work on variables assigned to objects of some specific type you introduce. This was already mentioned by This limitation was introduced for efficiency of the pattern-matcher/evaluator. For instance, I've defined a_ \ b_ := KroneckerProductĪ_ \ b_ \ c_ := a \ ( b \ c )Īnd a_ \ b_ := BlockDiagonal[ For these operators, you can define functions as you wish.

Some of Mathematica's operators do not have any behavior associated with them, so they're not protected. TagSet handles that by explicitly naming the appropriate Head, and in general, it is what I tend to prefer over UpSet. Secondly, from a coding perspective UpSetDelayed is easier to write, but occasionally there is some ambiguity as to which Head is the upvalue associated with. This may require you to get creative with an intermediate type. Plus is fine, but Exp] will throw an error. First, the upvalue mechanism can only scan one level deep, i.e. There are a couple of issues to be aware of. Setting an upvalue is somewhat more correct from a conceptual point of view since mod is the one with the different property. Alternatively, we can associate the addition property with the mod object itself via TagSet mod /: mod + mod := mod Mod We could remove Unprotect Plus and our definition would then work, but this may cause problems with other definitions and as Plus accumulates more definitions, it would slow down. But, it generates the error SetDelayed::write : Tag Plus in a_mod + b_mod is Protected. Initially, we'd think that a_mod + b_mod := + b, 5] So, we want anything with Head mod to behave correctly. Let's say you want to have a set of variables that obey modulo 5 addition, i.e. This is a crucial difference and can be illustrated by a simple example.

The latter associates the definition with f, while TagSet ( /:) and UpSet ( ^= and its delayed version, ^:=) associate the definition with g. Actually, g /: f] := h is not equivalent to f] := h.
