Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I supose probably yes, but at the end I assume that internally mathematica has to use Set or SetDelayed. Am I right?

Background: I am developing a super simple phraser that takes a code description how objects should work and changes it slightly. Part of the code deals with assignments. They have to be handled in a somewhat special way.

The generic indexing works like

OBJ[id_]@f := f[id]

I noticed that during the assignments Mathematica does not bring OBJ[1]@f to the bitter end, i.e.,f[1]`; for example in

OBJ[1]@f = 1

the left hand side is not resolved to f[1].

I changed how Set and SetDelayed works for fields in objects, and would like to make sure that Mathematica always goes through my definitions:

OBJ /: Set[OBJ[id_]@f, rhs_] := (f[id] = rhs);
OBJ /: SetDelayed[OBJ[id_]@f, rhs_] := (f[id] := rhs);
OBJ /: OBJ[id_][f] := (f[id]);

the first two lines handle the cases like

OBJ[1]@f = 1;
OBJ[2]@f := 2;

while the third line handles the cases like

a = 2 * OBJ[1]@f + 1

of course, a combination is possible:

OBJ[1]@f = OBJ[2]@f + 1;

I am providing the background to the question, which should hopefully put the question in the right context. Hope someone can help.

share|improve this question
If you're asking whether there are other ways to change values than Set, yes, there are: AppendTo, Increment, AddTo, and probably many others I can't recall ... – Szabolcs Mar 3 '14 at 18:26
of course, a field could be a list. thanks! so the construct above simply sucks. there are 10000 ways to bypass it. – zorank Mar 3 '14 at 18:38
You have a typo in your definitions. You defined OJB while you're using OBJ elsewhere... Set has attribute HoldFirst, so that you can make assignments like a = 1 even when a has a value. Try using Evaluate on the LHS. – R. M. Mar 3 '14 at 18:39
thanks for spoting the typo. alas, the behavior has nothing to do with it. it is the way set and setdelayed behave. – zorank Mar 3 '14 at 18:47
@zorank I also explained why they behave that way in my previous comment... they have the HoldFirst attribute. An Evaluate should fix that. In the off-chance that you're using this to emulate some OO like concepts, I would recommend taking a look here: Once more on object orientation in Mathematica: does it have to be so hard? – R. M. Mar 3 '14 at 18:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.